使用大导航标题滚动 table 时如何管理导航栏的两个按钮?
How can i manage both button of navigation bar when scrolling table with large navigation title?
在这里我可以附上我在滚动时遇到的问题的屏幕截图,我想隐藏更多按钮或将该按钮作为第二个按钮放入导航栏,但它被覆盖到第一个。
我通过调用这个函数将按钮设置为加载。
func setUpNavigationMoreButton() {
let rightButton = UIButton()
let btnFilterImage = #imageLiteral(resourceName: "DotsThreeOutline")
rightButton.setImage(btnFilterImage, for: .normal)
rightButton.setTitleColor(.purple, for: .normal)
rightButton.addTarget(self, action: #selector(filterClick), for: .touchUpInside)
navigationController?.navigationBar.addSubview(rightButton)
rightButton.tag = 1
rightButton.frame = CGRect(x: self.view.frame.width, y: 0, width: 30, height: 30)
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])
}
您必须使用 UIBarButtonItem 而不是 UIButton。
let btnFilterImage = #imageLiteral(resourceName: "DotsThreeOutline")
let rightButton = UIBarButtonItem(image: btnFilterImage, style: .plain, target: self, action: #selector(filterClick))
navigationController?.navigationItem.rightBarButtonItem = rightButton
如果您有多个按钮
let rightButton1 = UIBarButtonItem(image: image1, style: .plain, target: self, action: #selector(filterClick))
let rightButton2 = UIBarButtonItem(image: image2, style: .plain, target: self, action: #selector(filterClick))
navigationController?.navigationItem.rightBarButtonItems = [rightButton1, rightButton2]
如果您需要hide/unhide大标题更改时的更多按钮。
class VController: UIViewController {
let rightButton = UIButton()
var observer: NSKeyValueObservation?
override func viewDidLoad() {
super.viewDidLoad()
self.observer = self.navigationController?.navigationBar.observe(\.bounds, options: [.new], changeHandler: { (navigationBar, changes) in
if let height = changes.newValue?.height {
if height > 44.0 {
//Large Title, unhide button
rightButton.isHidden = false
} else {
//Small Title, hide button
rightButton.isHidden = true
}
}
})
}
}
我通过调用这个函数将按钮设置为加载。
func setUpNavigationMoreButton() {
let rightButton = UIButton()
let btnFilterImage = #imageLiteral(resourceName: "DotsThreeOutline")
rightButton.setImage(btnFilterImage, for: .normal)
rightButton.setTitleColor(.purple, for: .normal)
rightButton.addTarget(self, action: #selector(filterClick), for: .touchUpInside)
navigationController?.navigationBar.addSubview(rightButton)
rightButton.tag = 1
rightButton.frame = CGRect(x: self.view.frame.width, y: 0, width: 30, height: 30)
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])
}
您必须使用 UIBarButtonItem 而不是 UIButton。
let btnFilterImage = #imageLiteral(resourceName: "DotsThreeOutline")
let rightButton = UIBarButtonItem(image: btnFilterImage, style: .plain, target: self, action: #selector(filterClick))
navigationController?.navigationItem.rightBarButtonItem = rightButton
如果您有多个按钮
let rightButton1 = UIBarButtonItem(image: image1, style: .plain, target: self, action: #selector(filterClick))
let rightButton2 = UIBarButtonItem(image: image2, style: .plain, target: self, action: #selector(filterClick))
navigationController?.navigationItem.rightBarButtonItems = [rightButton1, rightButton2]
如果您需要hide/unhide大标题更改时的更多按钮。
class VController: UIViewController {
let rightButton = UIButton()
var observer: NSKeyValueObservation?
override func viewDidLoad() {
super.viewDidLoad()
self.observer = self.navigationController?.navigationBar.observe(\.bounds, options: [.new], changeHandler: { (navigationBar, changes) in
if let height = changes.newValue?.height {
if height > 44.0 {
//Large Title, unhide button
rightButton.isHidden = false
} else {
//Small Title, hide button
rightButton.isHidden = true
}
}
})
}
}