作者:橄榄村 | 来源:互联网 | 2023-01-28 12:38
我正在使用iOS 11的大标题导航栏,但是当我添加一个条形按钮项时,它看起来很奇怪,位于与原始标题导航栏相同的位置.我想在标题很大时向下移动条形按钮项目,并在导航栏不再大时将其移回原始位置.这样做的最佳方式是什么?
这是显示条形按钮项目的奇怪位置的图像
我可以使用viewWillLayoutSubviews()动态获取导航栏高度,但我无法使用setTitlePositionAdjustment更改条形按钮项的位置
override func viewWillLayoutSubviews() {
guard let navbarHeight = self.navigationController?.navigationBar.frame.height else{ return }
}
Ali..
8
为了解决我自己的问题,我只添加了一个按钮作为导航栏的子视图,并为导航栏设置了右约束和底约束。现在,当导航栏更改大小时,该按钮将上下移动。但是,这要求您从该视图控制器显示segue的所有视图控制器中都必须删除按钮。因此,我向按钮添加了标记1,并将其从另一个视图控制器的超级视图中删除。这是解决它的最简单方法,我发现它是最简单的方法。
设置右键:
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函数中调用
1> Ali..:
为了解决我自己的问题,我只添加了一个按钮作为导航栏的子视图,并为导航栏设置了右约束和底约束。现在,当导航栏更改大小时,该按钮将上下移动。但是,这要求您从该视图控制器显示segue的所有视图控制器中都必须删除按钮。因此,我向按钮添加了标记1,并将其从另一个视图控制器的超级视图中删除。这是解决它的最简单方法,我发现它是最简单的方法。
设置右键:
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函数中调用