Modal status bar and navigation bar text colors from UIActivityViewControllers in iOS 7
Asked Answered
F

6

17

When I'm using a UIActivityViewController, after the user chooses an activity (such as Mail or Message), I can not change the text color for the status bar nor the text/tint color of the Cancel and Send navigation bar buttons. For the bar buttons, in the AppDelegate I've tried using:

    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

And nothing happens. However I am able to set the navigation bar title with this:

    [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor, nil]];

I set the UIViewControllerBasedStatusBarAppearance to NO in the Info.plist. And put the line:

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

in the AppDelegate, and have had no luck changing the status bar color at all. Any ideas?

Freeholder answered 5/11, 2013 at 16:47 Comment(3)
have tried this for status bar color #17679381Taxiplane
@Taxiplane I have tried that. It only changes the status bar colors for my regular UIViewControllers and has no affect on the status bar of an activity that was launched from a UIActivityViewControllerFreeholder
I asked Apple for help on this one and they confirmed that this is a bug. I've filed a bug report, so hopefully this will get fixed in iOS 7.1. I would suggest filing a bug report as well at bugreport.apple.com and reference mine, 15959753.Knacker
E
7

As the UIActivityViewController presents the underlying model view controllers, we use this workaround to fix the status bar color issue:

@interface StatusBarColorApplyingActivityViewController : UIActivityViewController

@end

@implementation StatusBarColorApplyingActivityViewController

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
  [super presentViewController:viewControllerToPresent animated:flag completion:^{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    if (completion) {
      completion();
    }
  }];
}

@end

As you can see, this is just a class extending the UIActivityViewController overriding the presentViewController:animated:completion:. When the view controller has been presented we set the status bar style via UIApplication in the completion block. Then we call the original completion block given to the method, if any.

Exult answered 23/7, 2014 at 11:19 Comment(2)
In iOS 8 overriding presentViewController:animated:completion didn't work so instead I overrode viewWillAppear and viewWillDissappear to change back and forth between styles.Br
And how status bar color refers to the question?Crematory
G
4

Rather than sub-classing from UIActivityViewController, we can change the tintColor of the navigation bar upon presenting it and revert it upon completion in the completionHandler. For example:

UIColor *normalColor = [[UINavigationBar appearance] tintColor];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
[activityViewController setCompletionHandler:^(NSString *activityType, BOOL completed) {
      // back to normal color
     [[UINavigationBar appearance] setTintColor:normalColor];
}];

[self presentViewController:activityViewController animated:YES completion:^{
     // change color to suit your need
     [[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:25.0f/255.0f green:125.0f/255.0f blue:255.0f/255.0f alpha:1.0f]]; // ActionSheet options' Blue color
}];
Greyhound answered 2/9, 2014 at 12:36 Comment(0)
G
2

I believe the code to change the navigation bar color is this:

[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];

This is for changing the colours of the navigation bar buttons in iOS 7, if you might need it:

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

And this is the code if you want to change the colours of the buttons in iOS 6:

[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];

This code is also for iOS 8+ to change the bar button text color for UIActivityViewController activities (like sharing via Messages or Mail Composer).

Garb answered 21/11, 2013 at 12:12 Comment(2)
I'm developing an app for iOS 8+, and just FYI that last one (UIBarButtonItem appearance setTintColor) was needed in my app to change the bar button text color for UIActivityViewController activities (like sharing via Messages or Mail Composer).Wylie
@MasonG.Zhwiti Thanks for the response. I added it to my answer.Garb
B
2

In iOS 8 the UIActivityViewController presents its individual compose controllers on the root view controller of your application.

You need to subclass your root view controller (whether it be a UIViewController or UINavigationController) and add the following code.

@interface UINavigationControllerBarColor : UINavigationController

@end

@implementation UINavigationControllerBarColor

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    [super presentViewController:viewControllerToPresent animated:flag completion:^{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        if (completion) {
            completion();
        }
    }];
}

@end

and then instead of initializing a UINavigationController in the AppDelegate or storyboard, initialize your newly subclassed controller.

Some other recommendations subclass the UIActivityViewController but this does not work.

If you want to change the bar button and title colors as well use the following in your application:didFinishLaunching:

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                      [UIColor whiteColor], UITextAttributeTextColor,
                                                      [UIFont systemFontOfSize:18.0f], UITextAttributeFont,
                                                      nil]];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor whiteColor]];
Boult answered 7/2, 2015 at 2:4 Comment(2)
I did what you suggest in the first part, but it never gets called. I already had my root view controller subclassed to handle shouldAutorotate which works well, but presentViewController is not being called. Any other details?Traveler
It's the root of the top most presented view controller.Phenice
S
0

I found a solution to change the text color of the Send and Cancel buttons.

Check my answer from here.

Regarding changing the status bar style from black to white, I've tried pretty much everything that is on Stackoverflow and nothing worked. There seems to be no workaround it.

One thing that might work, but I don't really know how to use it, could be changing the status bar style in the child view controller. There's a Stackoverflow post about it here.

This might work only if the assumption that the MFMailComposerViewController and MFMessageComposeViewController are child view controllers of UIActivityViewController and therefore if we specify the status bar style for the UIActivityViewController then the child view controllers should have the same status bar style as the parent.

There's a method in the UIViewController called childViewControllerForStatusBarStyle. Here is the Apple documentation for it .

But I don't really know how to use that. Did anyone figure this out?

Studley answered 12/4, 2014 at 20:48 Comment(0)
T
-1

You have to set the tintColor of the entire app.

self.window.tintColor = [UIColor redColor];

or

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

bar buttons

self.navigationController.navigationBar.tintColor = [UIColor redColor];
Taxiplane answered 26/11, 2013 at 4:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.