ios - 使导航栏标题可快速点击

标签 ios swift uinavigationbar

我想让 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

在这里你可以看到在最后一行按钮直接设置为navigationItemtitleView,这将在导航栏的中心添加按钮。

按钮的操作方法如下:

@objc func clickOnButton() {
}

关于ios - 使导航栏标题可快速点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27632820/

相关文章:

ios - 如何在 Realm Swift 中将子类添加到 List<superclass>

ios - 如何在 Swift 中传递带有未知参数并返回值作为参数的函数

ios - 在 Swift 4 循环中保存和重绘

ios - 在 Xcode 6.1 中,我有两个导航按钮。但是它们在 Storyboard 上不再可见。我怎样才能让他们回来?

uinavigationcontroller - UINavigationBar多行标题

ios - 当第一个 View Controller 在 iOS 中隐藏其导航栏时,向后滑动会导致导航栏立即隐藏(不平滑)

ios - Cocos2d:在运行时为Sprite节点中批处理的CCSprite设置zOrder似乎无效

ios - UITableViewCell didSelectRowAtIndexPath 在响应选择时非常慢

ios - 从另一个类访问一个类变量的值 - Objective-C

swift - 清晰的导航栏,没有半透明