Modal status bar and navigation bar text colors fr

2019-02-08 03:52发布

问题:

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?

回答1:

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.



回答2:

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
}];


回答3:

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]];


回答4:

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).



回答5:

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?



回答6:

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];