I have an appearance proxy that sets the barTintColor
property to green on UINavigationBar
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:54./255 green:165./255 blue:53./255 alpha:1]];
As needed I override this using appearanceWhenContainedIn:
[[UINavigationBar appearanceWhenContainedIn:[INFSearchViewController class], nil] setBarTintColor:[UIColor colorWithWhite:0.80 alpha:1]];
This works fine.
However when I present an MFMessageComposeViewController
it adheres to the UINavigationBar
proxy and looks like the following.
Which obviously looks terrible, I would prefer MFMessageComposeViewController
to not adhere to the proxy but attempting to do
[[UINavigationBar appearanceWhenContainedIn:[MFMessageComposeViewController class], nil] setBarTintColor:[UIColor whiteColor]];
has no affect.
What course of action should I take here?
The hacky way: set the appearance back to the default white, present the modal, set the appearance to styled when the modal returns.
Or, reverse your thinking. Leave the global appearance as the default. Then you can selectively apply the styled nav bar where appropriate.
If "where appropriate" ends up being 90% of the app, just set up a thin subclass of UIViewController (or whatever view controller you use a lot) and that use that where you want the appearance.
[[UINavigationBar appearanceWhenContainedIn:[MyStyledViewController class], nil]
setBarTintColor:[UIColor colorWithRed:54./255 green:165./255 blue:53./255 alpha:1]];
And in each .h file, set your view controller superclass to MyStyledViewController
rather than plain old UIViewController
.
After digging around and trying a few different suggestions I arrived at a nice, non-hacky solution using a UINavigationController subclass.
This allows me to style all wanted nav bars once using the appearance proxy, with the exception of the MFMessageComposeViewController
and MFMailComposeViewController
which I'd prefer to look standard in order to communicate to the user that they are using core iOS functionality.
1 - Create a UINavigationController
subclass.
2 - Style your nav bar using the appearance proxy as you were, but now using appearanceWhenContainedIn:
[[UINavigationBar appearanceWhenContainedIn:[KCStyledNavController class], nil] setBarTintColor:[UIColor redColor]];
[[UINavigationBar appearanceWhenContainedIn:[KCStyledNavController class], nil] setTintColor:[UIColor whiteColor]];
3 - Go into your storyboard, select all the the UINavigationControllers
you want styled and change their custom class to your styled one.
来源:https://stackoverflow.com/questions/19017305/mfmessagecomposeviewcontroller-appearance-ios-7