更新到Xcode 9后导航栏按钮中的图像无法渲染

时间:2017-10-12 07:06:04

标签: ios swift xcode uinavigationbar xcode9

我在UIViewController导航栏的右侧有两个UIButton,UI按钮有图像。该应用程序工作正常,直到它在Xcode 8中运行,但是当我更新Xcode 9它没有渲染时,它采用整个导航栏。 在Xcode 8中它是 enter image description here

但在更新到Xcode 9后,它看起来像这样

enter image description here

我设置导航栏的代码是......

  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)

}

我很困惑为什么会这样。

1 个答案:

答案 0 :(得分:2)

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