【问题标题】:UIBarButtonItem(customView:) -- not tappableUIBarButtonItem(customView:) -- 不可点击
【发布时间】:2017-07-27 08:10:44
【问题描述】:

我在viewDidLoad 中有以下内容。它设置了一个自定义的rightBarButtonItem(在一个自定义的UINavigationBar 中)。

    let button = UIButton()
    let attrs = [NSFontAttributeName: UIFont(name: "Montserrat-Light", size: 14)!, NSForegroundColorAttributeName: UIColor.black]
    let title = NSAttributedString(string: "Sign In", attributes: attrs)
    button.setAttributedTitle(title, for: .normal)
    button.addTarget(self, action: #selector(self.pushVcLogin), for: .touchUpInside)

    let parentView = UIView(frame: button.bounds)
    parentView.addSubview(button)
    button.sizeToFit()
    parentView.bounds = CGRect(x: 0, y: 0, width: button.bounds.size.width, height: button.bounds.size.height)

    self.navItem.rightBarButtonItem = UIBarButtonItem(customView: parentView)

自定义的UIBarButtonItem 看起来应该是这样,但它不可点击。

有什么想法吗?

【问题讨论】:

  • 可以分享一下“pushVcLogin”的代码吗?
  • “自定义UINavigationBar”是什么意思?您是否使用UINavigationController?只是“自定义”的视觉外观,还是您使用的是独立的导航栏?
  • 对不起,我想通了——整个视图控制器(提示@NicolasMiari 的问题)嵌套在 UINavigationController 中,并且该控制器(无形地)将其 UINavigationBar 叠加在我的整个视图上,包括我的自定义 UINavigationBar。

标签: ios swift uinavigationbar uibarbuttonitem


【解决方案1】:

首先,如果你的视图层次有 UINavigationController

其次,您可以使用 navigationItem 并在其上添加自定义 UIBarButtonItem。

我刚刚检查了以下代码,它可以工作。

    let button = UIButton()        
    let attrs = [NSFontAttributeName: UIFont(name: "Montserrat-Light", size: 14)!, NSForegroundColorAttributeName: UIColor.black]
    let title = NSAttributedString(string: "Sign In", attributes: attrs)
    button.setAttributedTitle(title, for: .normal)
    button.addTarget(self, action: #selector(self.pushVcLogin), for: .touchUpInside)

    let parentView = UIView(frame: button.bounds)
    parentView.addSubview(button)
    button.sizeToFit()
    parentView.bounds = CGRect(x: 0, y: 0, width: button.bounds.size.width, height: button.bounds.size.height)

    self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: parentView)

这里也是故事板图像 navigationController 和 viewController 的样子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多