这里是我的代码上UIToolBar的UIButton的似乎不工作
override func viewDidLoad() {
super.viewDidLoad()
let button1 = UIButton(frame: CGRect(x: 50, y: 50, width: 30, height: 30))
button1.setTitle("hi", for: .normal)
button1.setTitleColor(.blue, for: .normal)
button1.setTitleColor(.black, for: .highlighted)
button1.addTarget(self, action: #selector(self.barItem2Clicked(sender:)), for: .touchUpInside)
button1.backgroundColor=UIColor.red
let barButton1 = UIBarButtonItem(customView: button1)
toolBar1.items?.append(barButton1)
}
方法barItem2Clicked
:
func barItem2Clicked(sender :Any?) {
NSLog("hello")
}
我想用UIButton的加入UIToolBar,但button1
的作用不能被称为,并且突出显示的效果也不显示。
我点击了我刚刚添加的button1
,但方法barItem2Clicked
从未调用过。
我错过了什么吗?
感谢
将目标添加到酒吧按钮项目时会发生什么?酒吧按钮项目的框架是什么? – powerj1984
你没有任何关于你的UIButton的参考。看到这里:https://stackoverflow.com/a/45385689/6167296 – MH175
谢谢你们,我认为我的代码按钮的框架不是这个问题的主要原因,我改变了框架'CGRect(x:50 ,y:50,width:30,height:30)'到'CGRect(x:0,y:0,width:50,height:50)',toolBar可以自动修复它。 – Loyoan