6

How do you use this new object to customize the navigation bar in iOS 13? I tried the following code in Objective-C but it's not working correctly. It only shows my title text attributes while a view controller is being pushed or popped on to the navigation stack.

UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
appearance.titleTextAttributes = @{NSFontAttributeName: font};

Here is the documentation for this object. https://developer.apple.com/documentation/uikit/uinavigationbarappearance?language=objc

Berry Blue
  • 15,330
  • 18
  • 62
  • 113

3 Answers3

16

It isn't enough to just create an instance of UINavigationBarAppearance. You have to actually set it on a UINavigationBar instance (or its appearance proxy).

// Setup the nav bar appearance
UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
appearance.titleTextAttributes = @{NSFontAttributeName: font};

// Apply it to a specific nav bar
someNavBarInstance.standardAppearance = appearance;
// There are also the compactAppearance and scrollEdgeAppearance properties that can be set as needed.

If you want this same customization on all nav bars in the app, apply it to the UINavigationBar.appearance proxy.

UINavigationBar.appearance.standardAppearance = appearance;
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Ok, thank you. How exactly do I do `UINavigationBar.appearance` in Objective-C? – Berry Blue Sep 25 '19 at 15:58
  • See my updated answer. Naming your variable `appearance` makes that code a little confusing. – rmaddy Sep 25 '19 at 16:01
  • Ok, that seems to work! The only problem is it's not keeping my font size during animation for pushing and popping views. Is font size not supported anymore with appearance? – Berry Blue Sep 25 '19 at 16:03
  • Maybe you have different settings for different nav bars. Note that if you want the whole app to use the same settings and you use the `UINavigationBar.appearance` proxy, make sure you set that in the app delegate before any code is run or any UI is created. The appearance proxy only applies to UI elements created after it is set. – rmaddy Sep 25 '19 at 16:06
  • Thanks, yes that's what I'm doing but it's not working. Thanks for helping me get this far at least. – Berry Blue Sep 25 '19 at 22:35
  • If you have `prefersLargeTitles` enabled, you'll also want to call `setLargeTitleTextAttributes` – lewis Oct 11 '19 at 15:03
  • I think this needs to be updated; `UINavigationBar.appearance` is failing to compile with an "ambiguous reference" error in iOS 13 + Swift 5 – Zack Jan 30 '20 at 16:22
  • You saved my night dude. – Eray Alparslan Mar 12 '21 at 21:10
2

For me, even though I correctly set the appearance to the current UINavigationBar, it was still not working.

I found out that you need to call navigationBar?.setNeedsLayout(), if you set an updated appearance on a UINavigationBar that is already being displayed.

ph1lb4
  • 1,982
  • 17
  • 24
-1

in iOS13 you need to set the title color on the UINavigationBarAppearance object like here (Objective C version):

UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
appearance.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
self.navigationController.navigationBar.standardAppearance = appearance;
self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
Vladyslav Panchenko
  • 1,517
  • 1
  • 19
  • 23