Ati*_*ukh 18 uinavigationbar ios swift
我想让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]
}
Run Code Online (Sandbox Code Playgroud)
但它给了我错误 Variable subview inferred to have type AvyObject, which may be unexpected
Kam*_*pai 64
另一种添加按钮作为导航控制器标题的方法.
您需要将导航项标题视图设置为按钮对象
在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
Run Code Online (Sandbox Code Playgroud)
在这里,您可以在最后一行按钮看到的是直接设置到titleView的navigationItem,这将在导航栏的中心添加按钮.
按钮的动作方法如下:
@objc func clickOnButton() {
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16013 次 |
| 最近记录: |