9

I am working on iOS8 App Extension (Photo Editing Extension)

I have tried these method to update the Navigation Bar color, but failed:

[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
[[UINavigationBar appearance] setTintColor:[UIColor blueColor]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor blueColor]];

It displays a default translucent gray nav bar.

default nav. color

Does anybody have idea on how to change the navigation bar color in iOS8 extension?

Popeye
  • 11,839
  • 9
  • 58
  • 91
David Ng
  • 470
  • 4
  • 13

3 Answers3

10

Try self.navigationController.navigationBar.barTintColor = UIColor.yellow first. This should work for some host apps but not all. Because some host apps configure the colors in UIAppearance settings.

I found some info in here: https://pspdfkit.com/blog/2017/action-extension/#uiappearance
According to the link above, the extension will "picks up the UIAppearance settings from its host app" and this has a higher priority than the "setColor" message you send to the instance.

In this case what you can do is to modify the plist of the extension:
In NSExtension dictionary you can add a key NSExtensionOverridesHostUIAppearance and set value to YES. This will make your extension override the UIApprearance setting of the host app. Unfortunately this is only available in iOS 10 and later.

Hope you find it helpful.

jokeman
  • 1,277
  • 9
  • 22
  • 1
    this should be marked as correct answer - `NSExtensionOverridesHostUIAppearance` solves the problem. – abjurato Feb 15 '19 at 06:16
0

Try setting the translucency of the UINavigationBar to NO like below, I believe then it should start picking up the colours

[[UINavigationBar appearance] setTranslucent:NO];

Here's the Apple Documentation for UINavigationBar translucent property

Popeye
  • 11,839
  • 9
  • 58
  • 91
  • It does not work.. I think the problem is `[UINavigationBar appearance]` isn't the way to access the title bar in extension.. – David Ng Sep 03 '14 at 13:05
  • @DavidNg Try `[[[[self navigationController] navigationBar] appearance] setTranslucent:NO];` and replace the rest of them with the same. – Popeye Sep 03 '14 at 14:54
  • [[self navigationController] navigationBar] does not have appearance property, appearance is a instanceType from `UINavigationBar` class. – David Ng Sep 04 '14 at 07:55
  • @DavidNg balls yeah forgot sorry. What happens when you try setting it directly and not using `appearance`? – Popeye Sep 04 '14 at 08:04
  • Not working. I guess it might be the navigation bar is not accessed in this way – David Ng Sep 04 '14 at 08:05
  • This confuses me because I don't believe they have changed anything around this going from iOS7 to iOS8 so your code should really work also see these http://stackoverflow.com/questions/17014713/how-to-change-uinavigationbar-background-color-from-the-app-delegate and http://stackoverflow.com/questions/18929864/how-to-change-navigation-bar-color-in-ios-7 – Popeye Sep 04 '14 at 08:37
  • Yes, but i think it's because it's contained as an extension. Thanks for your keen respond @Popeye! – David Ng Sep 04 '14 at 09:14
  • I'm not sure if it is the same case as Action Extension. It isn't possible to change navigation bar the same way as well. I had to use the old method of changing the navigation bar background color. Appearance methods, barTintColor, does not work. It's as though it's running a few OS version behind. – honcheng Sep 26 '14 at 02:35
  • @honcheng It does seem odd that this doesn't work I have also tried this code since answering and it doesn't work using appearance but I can set it doing it the other way still seems like a bit of a bug to me but I can live with it. – Popeye Sep 26 '14 at 07:45
-2

I have put your code in the appDelegate didFinishLaunchWithOption

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];
    [[UINavigationBar appearance] setTintColor:[UIColor blueColor]];
    [[UINavigationBar appearance] setBackgroundColor:[UIColor blueColor]];
    return YES;
}

And works...

enter image description here

FedeH
  • 1,343
  • 18
  • 24