This question already has answers here:
navigation bar rightbaritem image-button bug iOS 11
(12个答案)
两年前关闭。
我在UIViewController导航栏的右边有两个ui按钮,ui按钮有图像。在Xcode8中运行之前,应用程序运行得很好,但是当我更新Xcode9时,它并没有呈现,它占据了整个导航栏。
在Xcode 8中

但是在更新到Xcode 9之后

我设置导航栏的代码是。。。
我很困惑为什么会这样。
同样适用于profileBtn。
(12个答案)
两年前关闭。
我在UIViewController导航栏的右边有两个ui按钮,ui按钮有图像。在Xcode8中运行之前,应用程序运行得很好,但是当我更新Xcode9时,它并没有呈现,它占据了整个导航栏。
在Xcode 8中
但是在更新到Xcode 9之后
我设置导航栏的代码是。。。
func setUpNavBar(){
self.navigationController?.navigationBar.isTranslucent = false
self.navigationItem.setHidesBackButton(true, animated: true)
let notificationBtn = UIButton(type: .custom)
notificationBtn.setImage(UIImage(named: "notificationIcon"), for: .normal)
notificationBtn.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
notificationBtn.addTarget(self, action: #selector(HomeViewController.notificationClicked), for: .touchUpInside)
let item1 = UIBarButtonItem(customView: notificationBtn)
let profileBtn = UIButton(type: .custom)
profileBtn.setImage(UIImage(named: "user_profile"), for: .normal)
profileBtn.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
profileBtn.addTarget(self, action: #selector(HomeViewController.ProfileClicked), for: .touchUpInside)
let item2 = UIBarButtonItem(customView: profileBtn)
self.navigationItem.setRightBarButtonItems([item1,item2], animated: true)
}
我很困惑为什么会这样。
最佳答案
在iOS 11
中,您需要添加/设置高度约束,并使用
通知
let widthConstraint = notificationBtn.widthAnchor.constraint(equalToConstant: 35)
let heightConstraint = notificationBtn.heightAnchor.constraint(equalToConstant: 35)
heightConstraint.isActive = true
widthConstraint.isActive = true
同样适用于profileBtn。
关于ios - 更新到Xcode 9后,导航栏中按钮中的图像未呈现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46703423/