无法点击自定义添加的 UIBarButtonItem

Can't click custom added UIBarButtonItem

我有这个透明的导航栏,我可以添加一个自定义图标(下面的代码)。但是它似乎对点击没有反应,有人知道为什么会这样吗?

我将其添加到 UINavigationController 中的 UIViewController 中。

var button: UIButton = UIButton()
    button.setImage(UIImage(named: "customBack"), forState: .Normal)
    button.frame = CGRectMake(0, 0, 40, 40)
    button.imageEdgeInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    button.targetForAction("doAction:", withSender: button)
    button.backgroundColor = UIColor.brownColor()

    var leftItem:UIBarButtonItem = UIBarButtonItem()
    leftItem.customView = button
    self.navigationItem.leftBarButtonItem = leftItem
    self.navigationItem.leftBarButtonItem!.action = "doAction:"

    self.navigationController!.navigationItem.leftBarButtonItem = leftItem

我知道我不应该自己添加回项目,但在这种情况下确实需要这样做。该图标是在加载视图或 w/e.

之前即时添加的

总是在发布后我发现:

我正在使用

button.targetForAction("doAction:", withSender: button)

而我应该做的:

button.addTarget(self, action: "doAction:", forControlEvents: .TouchUpInside)

试一试...

var ysBackButton:UIButton = UIButton(frame:CGRectMake(0, 0, kNavBtnSize, kNavBtnSize))
ysBackButton.setImage(UIImage(named: kBack), forState: UIControlState.Normal)
ysBackButton.addTarget(self, action: Selector("backButtonAction"), forControlEvents:UIControlEvents.TouchUpInside)
var leftBarButton:UIBarButtonItem = UIBarButtonItem(customView: ysBackButton)
self.navigationItem.leftBarButtonItem = leftBarButton