代码之家  ›  专栏  ›  技术社区  ›  Loyoan

UIToolBar上的UIButton似乎不起作用

  •  1
  • Loyoan  · 技术社区  · 8 年前

    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")
    }
    

    button1 无法调用,高亮显示的效果也不会显示。

    按钮1 我只是补充了一下,但是方法 从未打过电话。

    我错过什么了吗?

    1 回复  |  直到 8 年前
        1
  •  0
  •   sarosh mirza    8 年前

    我刚刚试过你的代码,它似乎在工作。

    enter image description here