I would like to make the navigationbar
title to be clickable. When user click on it, it should perform a segue. But I have no idea how to do this.
I have tried the following to get the title and apply the Tap gesture to it.
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]
}
but its giving me error Variable subview inferred to have type AvyObject, which may be unexpected
To get rid of that error, specify a type for your
if let
constant. I'd also recommend changing thatif let
constant name since it's the same as the one already declared in the previous line.Kampai's answer updated for Swift 3:
One more approach to add button as a title of navigation controller.
You need to set navigation item title view to your button object
Create button object in
viewDidLoad()
method:Swift 4.0 Edit
Here you can see in last line button is directly set to the
titleView
ofnavigationItem
which will add button at the center of navigation bar.Action method for button is below: