使用iOS 11的大型标题时,调整条形按钮项的位置

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

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

largeTitleNavbar

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

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

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

要设置右键:

 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视图控制器中删除它:

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

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

您要做的是设置BarButtonItem的标题位置调整。 viewWillAppear下行添加到viewWillAppear func中。 使用verticalhorizontal值进行播放以获得您喜欢的layout

navigationItem.rightBarButtonItem?.setTitlePositionAdjustment(.init(horizontal: 10, vertical: 20), for: UIBarMetrics.default)

https://developer.apple.com/documentation/uikit/uibarbuttonitem/1617149-settitlepositionadjustment

好的方法是你可以调整导航标题,如果它很大,你的酒吧按钮会自动调整。 这是代码。 iOS邮件应用程序也会做同样的事情供您参考。

 func adjustsTitle() { guard let font = UIFont(name: "Helvetica-Medium", size: 16) else { return } let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 20)) label.textColor = UIColor.black label.textAlignment = .center label.text = navigationItem.title label.adjustsFontSizeToFitWidth = true navigationItem.titleView = label } 

更新的答案如果要调整标题下方的按钮,如果它增长,那么在这种情况下,您需要在导航栏上加载自定义视图。

 //Hide back button. Since you are going to have custom button navigationItem.hidesBackButton = true //Increase the height based on your view intrinsic content size navigationController?.navigationBar.frame.size.height = 100 guard let yourCustomView = UINib(nibName: "yourCustomXib", bundle: nil).instantiate(withOwner: nil, options: nil).first as? YourCustomView else { fatalError("Missing yourCustomXib") } navigationController?.navigationBar.addSubview(yourCustomView)