Remove Custom Nav Bar on Email Sheet - ios

I have this code in my App Delegate:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"Nav Bar.png"] forBarMetrics:UIBarMetricsDefault];
This works great, but I use the MFMailComposeViewController and I want it to have the default NavigationBar appearance.
How do I do that?
EDIT:
I tried this code:
[[UINavigationBar appearanceWhenContainedIn: [MFMailComposeViewController class], [UIViewController class], nil] setBackgroundImage:[UIImage imageNamed:#"Textured Background.png"] forBarMetrics:UIBarMetricsDefault];
I've also tried having only this code. Nothing changes. Default Nav bar, including with the Mail View Controller.
I think it could be something with the appearanceWhenContainedIn:. Does anyone know what MFMailComposeViewController would be contained in?

I figured it out! Here's the code:
[[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
MFMailComposeViewController *emailVC = [[MFMailComposeViewController alloc] init];
//the rest of the implementation goes here...
[self presentViewController:emailVC animated:YES completion:nil];
Then, I set the nav bar appearance back to normal here:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"Nav Bar.png"] forBarMetrics:UIBarMetricsDefault];
[self dismissViewControllerAnimated:YES completion:nil];
}

You can try this:
[UINavigationBar appearanceWhenContainedIn: [MFMailComposeViewController class], nil]
Which means all navigation bars contained in a MFMailComposeViewController class
From the docs: UIAppearance
This will return the appearance proxy so you can modify it like this:
[[UINavigationBar appearanceWhenContainedIn: [MFMailComposeViewController class], nil] setBackgroundImage:myImage];
Hope that helps.

Related

Cannot set default background image for MFMailComposeViewController

I'm using a MFMailComposeViewController to compose a mail and add an attachment in my app.
I've got this code in my AppDelegate.m
[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[UIImage new]];
[[UINavigationBar appearance] setTranslucent:NO];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0x0A/255.0f green:0x13/255.0f blue:0x1A/255.0f alpha:1]];
Then I'm using this code to create the MFMailComposeViewController in another UIViewController
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
[mailViewController.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
mailViewController.mailComposeDelegate = self;
[self presentViewController:mailViewController animated:YES completion:nil];
However, the UINavigationBar in the MFMailComposeViewController is dark, how come it doesn't "reset" itself to the background I specify before presenting it? Is it inheriting the color of the current UIViewController?
Set UINavigationBar's barTintColor to nil. It will remove the custom tint color.

DocumentInteractionController Navigation Bar Color

In my iOS application, I am using the DocumentInteractionController to preview the .csv document.
self.documentController = [UIDocumentInteractionController interactionControllerWithURL:fileLocation];
[self.documentController setDelegate:self];
[self.documentController presentPreviewAnimated:YES];
However, I am finding that the navigation bar is completely transparent. The back button is white, so it is not visible due to the white background.
Note that I have styled the navigation bar in the AppDelegate:
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0/255.0f green:138/255.0f blue:188/255.0f alpha:1.0f]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:#{NSFontAttributeName:[UIFont fontWithName:#"DINPro-Bold" size:17]}];
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:#"shadow"]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
Essentially, my question is how can I make the appearance of the navigation bar in the DocumentInteractionController View Controller consistent with the appearance of the navigation bar throughout the entire app (or at least visible!).
This line puts a transparent (or rather void) background image to your UINavigationBar. Why is that?
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
Just remove the line and everything works well.
If you want to set a shadow image, then you should think about using appearanceWhenContainedIn: instead of appearance so it won't spread to unhandled controllers.
As for the Status Bar Style, the simplest way would be to pass self.navigationController as the presenter, instead of self:
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
return self.navigationController;
}
Hope this will help,

UIAppearance subview disappears from Navigationbar

I'm doing some changes to the Navigationbar with appearance in appdelegate.
This is my method:
-(void) setAppearance{
NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
[titleBarAttributes setValue:[UIFont fontWithName:#"AvantGarde-ExtraLight" size:18] forKey:NSFontAttributeName];
[titleBarAttributes setValue:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
[[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:6.0/256.0 green:57.0/256.0 blue:84.0/256.0 alpha:1.0]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
int borderSize = 3;
UIImageView *navBorder = [[UIImageView alloc] initWithFrame:CGRectMake(0,
41,
320,
borderSize)];
navBorder.image = [UIImage imageNamed:#"energy_line"];
navBorder.tag = 999;
[[UINavigationBar appearance] addSubview:navBorder];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
I have a method in my appdelegate that sets the window to my first viewController, when i call this method, my navBorder is removed from the navigationbar. I don't understand why this happens, there is no code that changes anything in my navigationbar in the viewcontroller.
- (void)rootView
{
[self.window setRootViewController:initialViewController];
}
I answered pretty much the same question here: https://stackoverflow.com/a/26414437/538491
But here is a summary:
Calling [[UINavigationBar appearance] returns an appearance proxy for the receiver class. The addSubview: method is not tagged as UI_APPEARANCE_SELECTOR. One major downside to UIAppearance's proxy approach is that it's difficult to know which selectors are compatible.
You should get a hold of the navigation bar and add the image there by calling this method: [self.navigationController.navigationBar addSubview:navBorder] or you should subclass UINavigationBar which gives you more flexibility.

How can I add navigationbar background image to MFMailComposeViewController on ios7

I am using MFMailComposeViewController.I can't add navigationBar background image to MFMailComposeViewController on ios7.My Code working ios7 before .But doesn't work on ios7.How can I add navigationbar background image to MFMailComposeViewController on ios7?
MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
mailCompose.mailComposeDelegate = self;
[mailCompose setSubject:#"SubjectName"];
[mailCompose setMessageBody:shareBodyString isHTML:NO];
if ([self respondsToSelector:#selector(presentViewController:animated:completion:)])
{
[self presentViewController:mailCompose animated:YES completion:nil];
}
else {
[self presentModalViewController:mailCompose animated:YES];
}
[mailCompose.topViewController.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navigationbar-background.png"] forBarMetrics:UIBarMetricsDefault]; // working before ios7
Use the Appearance Proxy. Following code will target the whole application :
UINavigationBar *navBar = [UINavigationBar appearance];
[navBar setBackgroundImage:[UIImage imageNamed:#"navigationbar-background.png"] forBarMetrics:UIBarMetricsDefault];
If you prefer to target only in MFMailComposeViewController change the first line with this :
navBar = [UINavigationBar appearanceWhenContainedIn:[MFMailComposeViewController class], nil];
If you already customised navigation bar and also want it to apply MFMailComposeViewController, It only allows to do it with UIAppearance proxy. For iOS 7.1.1, i replaced navigation bar background but couldn't change status bar background. Also it greyed out bar button items on subsequent calls. Therefore I stop customising and tried to turn back to default navigation bar style before creating MFMailComposeViewController
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
[[UINavigationBar appearance] setBarStyle:UIBarStyleDefault];
[[UINavigationBar appearance] setTintColor:nil];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,[UIFont fontWithName:#"Helvetica-Bold" size:18.0], NSFontAttributeName, nil]];
MFMailComposeViewController *mailComposer =[[MFMailComposeViewController alloc] init];

How to customize UINavigationBar only for certain view controller

[[UINavigationBar appearance] setBackgroundImage:gradientImage44
forBarMetrics:UIBarMetricsDefault];
Would customize UINavigationBar
Say I have a class
BGImageBookController
I want UINavigationBar on top of BGImageBookController to have a different background. How would I do so?
I did this in viewDidload
- (void)viewDidLoad
{
[super viewDidLoad];
[[UINavigationBar appearanceWhenContainedIn:[BGImageBookController class], nil] setBackgroundImage:[UIImage imageNamed:#"sushi_eating_cat"] forBarMetrics:UIBarMetricsDefault];
}
no effect
This is your code:
[[UINavigationBar appearanceWhenContainedIn:[BGImageBookController class], nil] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
You just call setBackgroundImage:forBarMetrics: in the viewDidLoad method of the BGImageBookController.

Resources