我想让 navigationbar
标题可以点击。当用户点击它时,它应该执行一个 segue。但我不知道该怎么做。
我尝试了以下方法来获取标题并将 Tap 手势应用于它。
var subviews = self.navigationController?.navigationBar.subviews
if let subviews = subviews {
// Better check for array length before accessing to the 1st element
var subview = subviews [0]
}
但它给我错误 Variable subview inferred to have type AvyObject, which may be unexpected
最佳答案
另一种添加按钮作为导航 Controller 标题的方法。
您需要将导航项标题 View 设置为您的按钮对象
在 viewDidLoad()
方法中创建按钮对象:
Swift 4.0 编辑
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
button.backgroundColor = .red
button.setTitle("Button", for: .normal)
button.addTarget(self, action: #selector(clickOnButton), for: .touchUpInside)
navigationItem.titleView = button
在这里你可以看到在最后一行按钮直接设置为navigationItem
的titleView
,这将在导航栏的中心添加按钮。
按钮的操作方法如下:
@objc func clickOnButton() {
}
关于ios - 使导航栏标题可快速点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27632820/