简体   繁体   English

单击导航栏外垂直对齐的栏按钮时出现问题

[英]Issue with clicking vertically aligned Bar Buttons outside of the Navigation Bar

I am trying to align two UIBarButtonItem s vertically on the right side of my UINavigationBar .我试图在我的UINavigationBar的右侧垂直对齐两个UIBarButtonItem I am using a custom view, and aligning two buttons within that.我正在使用自定义视图,并在其中对齐两个按钮。

Here is the code:这是代码:

let rightBarButtonCustomView = UIView(frame: CGRect(x: 0, y: 0, width: 40, height: 90))
        
        let settingsButton = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
        settingsButton.setImage(UIImage(systemName: "gearshape.fill"), for: .normal)
        settingsButton.backgroundColor = preferredBackgroundColor
        settingsButton.tintColor = .accentColor
    
        settingsButton.addTarget(self, action: #selector(openSettingsScreen), for: .touchUpInside)
        settingsButton.layer.cornerRadius = settingsButton.frame.size.height / 2
        settingsButton.layer.borderWidth = 1
        settingsButton.layer.borderColor = UIColor.label.cgColor
        settingsButton.layer.masksToBounds = true
        
        rightBarButtonCustomView.addSubview(settingsButton)
        
        //NotificationButton
        let notificationsButton = UIButton(frame: CGRect(x: 0, y: 50, width: 40, height: 40))
        notificationsButton.setImage(UIImage(systemName: "bell.fill"), for: .normal)
        notificationsButton.backgroundColor = preferredBackgroundColor
        notificationsButton.tintColor = .accentColor

        notificationsButton.addTarget(self, action: #selector(openNotificationScreen), for: .touchUpInside)
        notificationsButton.layer.cornerRadius = notificationsButton.frame.size.height / 2
        notificationsButton.layer.borderWidth = 1
        notificationsButton.layer.borderColor = UIColor.label.cgColor
        notificationsButton.layer.masksToBounds = true
        
        rightBarButtonCustomView.addSubview(notificationsButton)
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBarButtonCustomView)

Here is what it looks like:这是它的样子: 图片1

Pretty good right?很不错吧?

Well, the issue is that the notification button is not receiving clicks, but the settings button is.好吧,问题是通知按钮没有收到点击,但设置按钮是。 My guess is that the notification button is outside of the navigation bar, as shown in this picture ( UINavigationBar is blue, my rightBarButtonCustomView is red):我的猜测是通知按钮在导航栏之外,如下图所示( UINavigationBar为蓝色,我的rightBarButtonCustomView为红色): 图片2

I have tried to use constraints, increase the height of the navigation bar, but nothing seemed to work.我尝试使用约束,增加导航栏的高度,但似乎没有任何效果。

Any help would be appreciated任何帮助,将不胜感激

Thanks!谢谢!

The below code is working well for me.下面的代码对我来说效果很好。

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    navigationBar.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 200)
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM