如果存在多个navigationItem.rightBarButtonItem,则无法在navigationItem.titleView的中央进行渲染?

时间:2019-03-03 09:03:48

标签: ios swift

我正在做this教程。由于this问题,我必须修复我的代码。导航栏正在渲染如下图。我只需要标题图标位于导航栏的中心即可。有什么花招吗?

enter image description here

这是我当前的代码。

func setUpNavigationBarItems(){
    //https://www.youtube.com/watch?v=zS-CCd4xmRY
    let titleImageView = UIImageView(image: UIImage(named: "ic_nav_app_icon"))
    titleImageView.frame = CGRect(x: 0, y: 0, width: 34, height: 34)
    titleImageView.contentMode = .scaleAspectFit
    navigationItem.titleView = titleImageView


    let addButton = UIButton(type: .system)
    let addImage = UIImage(named: "ic_nav_add")
    addButton.setImage(addImage?.withRenderingMode(.alwaysOriginal), for: .normal)
    NSLayoutConstraint(item: addButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 30).isActive = true
    NSLayoutConstraint(item: addButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 30).isActive = true
    navigationItem.leftBarButtonItem = UIBarButtonItem(customView: addButton)


    let searchButton = UIButton(type: .system)
    let searchImage = UIImage(named: "ic_nav_phone")
    searchButton.setImage(searchImage?.withRenderingMode(.alwaysOriginal), for: .normal)
    NSLayoutConstraint(item: searchButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 30).isActive = true
    NSLayoutConstraint(item: searchButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 30).isActive = true

    let settingButton = UIButton(type: .system)
    let settingImage = UIImage(named: "ic_nav_setting")
    settingButton.setImage(settingImage?.withRenderingMode(.alwaysOriginal), for: .normal)
    NSLayoutConstraint(item: settingButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 30).isActive = true
    NSLayoutConstraint(item: settingButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 30).isActive = true

    navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: searchButton), UIBarButtonItem(customView: settingButton)]

    navigationController?.navigationBar.backgroundColor = .white

}

1 个答案:

答案 0 :(得分:2)

如果替换,它应该具有正确的布局:

titleImageView.frame = CGRect(x: 0, y: 0, width: 34, height: 34)

使用:

NSLayoutConstraint(item: titleImageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 34).isActive = true
NSLayoutConstraint(item: titleImageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 34).isActive = true

如前一个答案所述,标题视图的框架在运行时可能不是34x34。相反,它部分地由图像的大小(UIImageView的固有内容大小取决于图像的大小)和UINavigationBar的“自动布局”配置确定。

如果运行视图调试器,则可能会看到标题视图的框架类似于150x44,这就是为什么将其偏移到一侧以为UINavigationBar中的所有空间腾出空间。

视图调试工具位于Xcode的底部栏中(在调试区域的顶部):

Debug View Hierarchy

它使您可以检查框架,约束和视图层次结构的更多内容,并且通常会提示您在遇到此类问题时可能出什么问题。