Overview
The UINavigationController / UINavigationBar title text no longer appears when updating a view controller's title property programatically.
Example
Given a view controller that is hosted within a UINavigationController:
final class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// start out with no title
}
private func updateTitle() {
// update the title later on ...
title = "Hello"
}
}
In this case the title label remains blank after calling updateTitle() instead of updating to "Hello!".
Details
- This only happens if the title starts out as
nilor empty string - Triggering a layout (e.g. rotating the screen) will restore the title
- Manually calling
navigationController?.navigationBar.setNeedsLayout()restores the title - Inspecting the view hierarchy, it appears the
UILabelwithin the navigation bar has an alpha of0.0which is why it appears blank even after setting the title, it's not till after triggering a layout the alpha is reset to1.0
(lldb) po navigationController?.navigationBar.perform(Selector("recursiveDescription"))
...
<_UINavigationBarTitleControl: 0x7f84f5f11560; frame = (172.333 10; 45.3333 23); layer = <CALayer: 0x6000039e89a0>>
| <UILabel: 0x7f84f5f12000; frame = (0 0; 45.3333 23); text = 'Hello'; alpha = 0; userInteractionEnabled = NO; backgroundColor = UIExtendedGrayColorSpace 0 0; layer = <_UILabelLayer: 0x600001ab4410>>
| <UIView: 0x7f84d5f0fbc0; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <CALayer: 0x6000039d7780>>
Environment
This only occurs when building with Xcode 14 beta 6 (14A5294g) and running on iOS 16 beta.
Notes
I was curious if anyone else encountered this?
We initially missed this as many of our view controller had some default titles set but there were a few where titles get set asynchronously after viewDidLoad which surfaced this issue.
I wonder if some of the changes related to SwiftUI listed in the release notes may have accidentally changed the behaviour:
By default, a navigation bar only renders if it has content to display. If a navigation bar has no title, toolbar items, or search content, it’s automatically hidden. Use the navigationBarHidden(_:) or the new .toolbar(.visible) modifier to explicitly show an empty navigation bar. (84996257)
I've filed a feedback FB11379313 with a sample project that reproduces this issue.
Thanks