无法执行单击导航栏自定义按钮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法执行单击导航栏自定义按钮相关的知识,希望对你有一定的参考价值。

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

enter image description here

我面临的问题是前两个按钮(“主页”和“事件”)可以很好地单击并做出相应的响应,但是第三个按钮(“今天”)将无法单击。我对为什么感到困惑,并认为也许这是通常导航栏标题所在的位置,因此可能存在一些冲突,但我似乎无法解决它]

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

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
}

我有一个带导航栏的ViewController,其中有3个自定义按钮,这些按钮已以编程方式分配了单击事件。导航栏如下所示:我正以某种方式面临的问题是...

答案

[viewLeftItem的宽度是120,第三个按钮的x的原点是120

以上是关于无法执行单击导航栏自定义按钮的主要内容,如果未能解决你的问题,请参考以下文章

uni-app APP端隐藏导航栏自定义按钮

从片段中的按钮单击打开导航抽屉(Kotlin)

导航抽屉活动:在按钮单击时从片段移动到片段

iPad导航栏自定义高度

Android espresso 无法导航到其他 Fragment?

iOS NavigationBar 导航栏自定义