使用iOS 11的大型标题时,调整条形按钮项的位置 [英] Adjust position of bar button item when using large titles with iOS 11

查看:93
本文介绍了使用iOS 11的大型标题时,调整条形按钮项的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iOS 11的大标题导航栏,但是当我添加一个条形按钮项时,它看起来很奇怪,位于与原始标题导航栏相同的位置。我想在标题很大时向下移动条形按钮项目,并在导航栏不再大时将其移回原始位置。这样做的最佳方式是什么?

I am using the large title navbar with iOS 11, but when I add a bar button item it looks weird positioned in the same location as the original title navbar. I would like to move the bar button item down when the title is large, and move it back into its original position when the navbar is no longer large. What would be the best way of doing this?

这是显示条形按钮项目奇怪位置的图像

This is an image showing the weird position of the bar button item

我可以使用viewWillLayoutSubviews()动态获取导航栏高度,但我无法更改条形图的位置按钮项目使用setTitlePositionAdjustment

I can get the navbar height dynamically using the viewWillLayoutSubviews(), but I can't change the position of the bar button item using setTitlePositionAdjustment

override func viewWillLayoutSubviews() {
    guard let navbarHeight = self.navigationController?.navigationBar.frame.height else{ return }
}


推荐答案

为了解决我自己的问题,我刚刚添加了一个按钮作为导航栏的子视图,并将右侧和底部约束设置为导航栏。现在,当导航栏改变大小时,该按钮将上下移动。但是,这需要在您从此视图控制器显示segue的任何视图控制器中删除该按钮。因此,我在按钮上添加了1的标记,并从其他视图控制器的superview中删除了它。这是解决它的最简单方法,我发现它是最简单的方法。

To solve my own problem, I just added a button as a subview of the navbar and set the right and bottom constraints to the navbar. The button will now move up and down when the navbar changes size. However, this requires the button to be removed in any view controllers that you show segue from this view controller. Thus, I added a tag of 1 to the button and removed it from its superview from the other view controller. This is the easiest way to solve it, and I found it the easiest method.

设置右键:

func setupNavBar() {

    self.title = "Home"
    self.navigationController?.navigationBar.prefersLargeTitles = true
    self.navigationController?.navigationBar.isTranslucent = false

    let searchController = UISearchController(searchResultsController: nil)
    self.navigationItem.searchController = searchController

    let rightButton = UIButton()
    rightButton.setTitle("Right Button", for: .normal)
    rightButton.setTitleColor(.purple, for: .normal)
    rightButton.addTarget(self, action: #selector(rightButtonTapped(_:)), for: .touchUpInside)
    navigationController?.navigationBar.addSubview(rightButton)
    rightButton.tag = 1
    rightButton.frame = CGRect(x: self.view.frame.width, y: 0, width: 120, height: 20)

    let targetView = self.navigationController?.navigationBar

    let trailingContraint = NSLayoutConstraint(item: rightButton, attribute:
        .trailingMargin, relatedBy: .equal, toItem: targetView,
                         attribute: .trailingMargin, multiplier: 1.0, constant: -16)
    let bottomConstraint = NSLayoutConstraint(item: rightButton, attribute: .bottom, relatedBy: .equal,
                                    toItem: targetView, attribute: .bottom, multiplier: 1.0, constant: -6)
    rightButton.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([trailingContraint, bottomConstraint])

}

要从任何show segued视图控制器中删除它:

To remove it from any show segued view controllers:

func removeRightButton(){
    guard let subviews = self.navigationController?.navigationBar.subviews else{return}
    for view in subviews{
        if view.tag != 0{
            view.removeFromSuperview()
        }
    }
} 

这两个函数都在viewWillAppear函数中调用

Both functions are called in the viewWillAppear function

这篇关于使用iOS 11的大型标题时,调整条形按钮项的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆