更新到Xcode 9后导航栏按钮中没有渲染图像
问题描述:
我在UIViewController的导航栏的右侧有两个UIButton,UIbuttons有图像。该应用程序工作正常,直到它在Xcode 8中运行,但是当我更新Xcode 9时,它不是呈现,而是整个导航栏。 在Xcode中8是 更新到Xcode 9后导航栏按钮中没有渲染图像
但更新到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
你需要添加/身高的一系列约束和与UIButton
对于notificationBtn
let widthConstraint = notificationBtn.widthAnchor.constraint(equalToConstant: 35)
let heightConstraint = notificationBtn.heightAnchor.constraint(equalToConstant: 35)
heightConstraint.isActive = true
widthConstraint.isActive = true
为profileBtn应用一样了。
我认为你应该标记为重复大声笑 –
它的作品感谢人 – Prathamesh