如何绘制与 UIBarButtonItem 具有完全相同样式的石英按钮。按钮应该能够显示不同的颜色。我下载了 Three20 项目,但是这个项目真的很复杂,你需要很多时间才能忽略整个框架。我只想绘制一个自定义 UIBarButtonItem。
感谢帮助。
如何绘制与 UIBarButtonItem 具有完全相同样式的石英按钮。按钮应该能够显示不同的颜色。我下载了 Three20 项目,但是这个项目真的很复杂,你需要很多时间才能忽略整个框架。我只想绘制一个自定义 UIBarButtonItem。
感谢帮助。
尝试只有一个段和瞬时选择的 UISegmentedControl。它支持 tintColor 并且可能足够接近您的目的。
如果您使用的是 Three20 库,我想 TTButton 的“工具栏按钮”样式就是您想要的。TTButton 是 UIButton 的子类,但它允许您在创建网页时像使用 css 一样应用样式。
要创建像 UIBarButtonItem 这样的按钮,只需调用
[TTButton buttonWithStyle:@"toolbarButton:" title:@"Title"]
您可能需要保留它,因为该按钮是一个自动释放对象。
在斯威夫特 4.1
您可以使用 Quartz2D 使用您自己的设计/动画/颜色/等添加您自己的自定义视图,如下所示。
此方法创建的条形按钮项不会调用其目标的操作方法来响应用户交互。相反,条形按钮项期望指定的自定义视图来处理任何用户交互并提供适当的响应。
不要忘记添加您的视图必须符合协议(CameraButtonDelegate
在这种情况下),并且还要添加cameraButtonChanged(to:)
捕获用户交互的方法
func setupButton() {
customView = CameraButton()
customView.frame = CGRect(x: 0, y: 0, width: 25, height: 25)
customView.delegate = self
let barButtonItem = UIBarButtonItem(customView:customView)
self.navigationItem.rightBarButtonItem = torchItem
}
protocol CameraButtonDelegate {
func cameraButtonChanged(to state:Bool)
}
class CameraButton: UIView {
var delegate: CameraButtonDelegate?
var active: Bool = false
var strokeColor = UIColor.gray
init() {
self.init()
self.backgroundColor = .clear
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.active = self.active ? false : true
self.strokeColor = self.active ? UIColor.blue : UIColor.gray
setNeedsDisplay()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.delegate?.cameraButtonChanged(to: self.active)
}
override func draw(_ rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
context?.setStrokeColor(strokeColor.cgColor)
context?.setLineWidth(1.0)
// Add your Quartz stuff here
context?.strokePath()
}
}