【问题标题】:Image in navigation bar button not rendering after updating to Xcode 9 [duplicate]更新到 Xcode 9 后导航栏按钮中的图像未呈现 [重复]
【发布时间】:2017-10-12 07:06:04
【问题描述】:

我在 UIViewController 的导航栏右侧有两个 UIButton,UIbuttons 有图像。该应用程序运行良好,直到它在 Xcode 8 中运行,但是当我更新 Xcode 9 时它没有呈现,它占用了整个导航栏。 在 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 swift xcode uinavigationbar xcode9


    【解决方案1】:

    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

    【讨论】:

    • 我认为你应该标记为重复大声笑
    • 谢谢你的工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    相关资源
    最近更新 更多