8

Why can not I set a title for my UINavigationController in Swift?

 2 years ago
source link: https://www.codesd.com/item/why-can-not-i-set-a-title-for-my-uinavigationcontroller-in-swift.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Why can not I set a title for my UINavigationController in Swift?

advertisements

I have a UINavigationController in my app and I added there the segmented control:

I also have a button and when user presses it, I want to hide the segmented control and display a string instead.

I can hide the segmented control by doing:

segmentedControl.hidden = true

and it works, but when I write in the code:

self.title = "test"

then the test word does not appear. What's the problem here and how can I fix it?


If you have already set titleView of navigationItem and trying to set title then you need to first set titleView as nil then only it will display your title on NavigationBar.

self.navigationItem.titleView = nil
self.navigationItem.title = "Test"

Edit: To get the instance of SegmentedControl back you need to first store the instance from titleView like this.

self.segmentedControl = self.navigationItem.titleView as! UISegmentedControl
self.navigationItem.titleView = UIView() //Set blank view so it will not nil the object of SegmentedControl
self.navigationItem.titleView = nil
self.navigationItem.title = "Test"


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK