【发布时间】:2011-01-16 11:50:27
【问题描述】:
如何绘制一个与 UIBarButtonItem 样式完全相同的带有石英的按钮。按钮应该能够显示不同的颜色。 我下载了 Three20 项目,但是这个项目真的很复杂,你需要很多时间才能忽略整个框架。我只想画一个自定义的 UIBarButtonItem。
感谢您的帮助。
【问题讨论】:
标签: iphone uibutton uibarbuttonitem quartz-2d uicontrol
如何绘制一个与 UIBarButtonItem 样式完全相同的带有石英的按钮。按钮应该能够显示不同的颜色。 我下载了 Three20 项目,但是这个项目真的很复杂,你需要很多时间才能忽略整个框架。我只想画一个自定义的 UIBarButtonItem。
感谢您的帮助。
【问题讨论】:
标签: iphone uibutton uibarbuttonitem quartz-2d uicontrol
尝试仅使用一个段和瞬时选择的 UISegmentedControl。它支持 tintColor 并且可能足够接近您的目的。
【讨论】:
如果您使用的是 Three20 库,我想 TTButton 的“工具栏按钮”样式就是您想要的。 TTButton 是 UIButton 的子类,但它允许您在创建网页时像使用 css 一样应用样式。
要创建像 UIBarButtonItem 这样的按钮,只需调用
[TTButton buttonWithStyle:@"toolbarButton:" title:@"Title"]
您可能需要保留它,因为按钮是一个自动释放对象。
【讨论】:
在 Swift 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()
}
}
【讨论】: