开发者社区> 问答> 正文

单击导航栏自定义按钮失败

我有一个带有导航条的ViewController,其中有3个自定义按钮,我已经编程地分配了单击事件。导航栏如下所示:

https://i.stack.imgur.com/3mypa.jpg

我面临的问题是,前两个按钮(“Home”和“Events”)可以很好地单击并相应地做出响应,但是第三个按钮(“今天”)不会单击。我对为什么感到困惑,并认为这可能是通常导航栏标题的地方,所以可能会有一些冲突,但我似乎无法解决

添加这些按钮并单击事件的方法如下:

func addCustomBtnsNavBar(){
    self.title = nil
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
    self.navigationController?.navigationBar.shadowImage = UIImage()

    let viewLeftItem = UIView(frame: CGRect.init(x: 0, y: 5, width: 120, height: 40))

    let button1a = UIButton(frame: CGRect.init(x: 0, y: 5, width: 50, height: 40))
    button1a.setTitle("Home", for: .normal)
    button1a.titleLabel?.font =  UIFont(name: "Nolasco Sans", size: 16)
    button1a.setTitleColor(.black, for: .normal)

    button1a.addTarget(self, action: #selector(self.openAboutUs), for: .touchUpInside)

    let button1b = UIButton(frame: CGRect.init(x: 60, y: 5, width: 50, height: 40))
    button1b.setTitle("Events", for: .normal)
    button1b.titleLabel?.font =  UIFont(name: "Nolasco Sans", size: 16)
    button1b.setTitleColor(Util.hexStringToUIColor(hex: Constants.lightGrey), for: .normal)
    button1b.addTarget(self, action: #selector(self.openEventsPage), for: .touchUpInside)

    let button1c = UIButton(frame: CGRect.init(x: 120, y: 5, width: 50, height: 40))
    button1c.setTitle("Today", for: .normal)
    button1c.titleLabel?.font =  UIFont(name: "Nolasco Sans", size: 16)
    button1c.setTitleColor(Util.hexStringToUIColor(hex: Constants.lightGrey), for: .normal)
    button1c.addTarget(self, action: #selector(self.openEventsPage), for: .touchUpInside)
    let leftBarButton = UIBarButtonItem(customView: viewLeftItem)

    let viewRightItem = UIView(frame: CGRect.init(x: 0, y: 0, width: 60, height: 30))

    let button2 = UIButton(frame: CGRect.init(x: 35, y: 0, width: 30, height: 30))
    button2.setBackgroundImage(UIImage(named: "blue_circle"), for: .normal)
    button2.setTitle(getInitials(), for: .normal)
    button2.titleLabel?.font =  UIFont(name: "Google Sans", size: 12)
    button2.setTitleColor(.white, for: .focused)
    button2.addTarget(self, action: #selector(self.didTapRightButton), for: .touchUpInside)

    viewLeftItem.addSubview(button1a)
    viewLeftItem.addSubview(button1b)
    viewLeftItem.addSubview(button1c)

    viewRightItem.addSubview(button2)

    let rightBarButton = UIBarButtonItem(customView: viewRightItem)

    self.navigationItem.leftBarButtonItem = leftBarButton
    self.navigationItem.rightBarButtonItem = rightBarButton
}

展开
收起
游客5akardh5cojhg 2019-12-25 16:13:13 2029 0
1 条回答
写回答
取消 提交回答
  • viewLeftItem的宽度为120,并且x您的第三个按钮的来源是120.

    我相信如果你能viewLeftItem氏clipToBounds财产true你根本看不见它。

    您必须将容器的宽度增加到170根据你的第三个按钮的来源和宽度。

    2019-12-25 16:13:29
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载