我有一个带有导航条的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
}
viewLeftItem的宽度为120,并且x您的第三个按钮的来源是120.
我相信如果你能viewLeftItem氏clipToBounds财产true你根本看不见它。
您必须将容器的宽度增加到170根据你的第三个按钮的来源和宽度。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。