Cannot set default background image for MFMailComposeViewController - ios

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.

Related

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.

MFMailComposeViewController bar background color not changing in iOS7

I'm trying to change the background color of the MFMailComposeViewController in iOS7 but I cannot make it work.
I'm using the following snipped:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
if([picker.navigationBar respondsToSelector:#selector(barTintColor)]) {
// iOS7
picker.navigationBar.barTintColor = READER_NAVIGATION_BAR_BACKGROUND_COLOR;
// Set back button arrow color
[picker.navigationBar setTintColor:READER_NAVIGATION_BAR_BACK_BUTTON_ARROW_COLOR];
// Set Navigation Bar Title Color
[picker.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:READER_NAVIGATION_BAR_TITLE_NORMAL_FONT_COLOR forKey:UITextAttributeTextColor]];
// Set back button color
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:READER_NAVIGATION_BAR_BUTTONS_FONT_COLOR, UITextAttributeTextColor,nil] forState:UIControlStateNormal];
}
Does anybody knows how to change the bakcground color of the MFMailComposeViewController in iOS7?
The trick here is to call 'appearance methods' such as
[UINavigationBar appearance].barTintColor = [UIColor whiteColor];
[UINavigationBar appearance].tintColor = [UIColor redColor];
BEFORE calling to
[[MFMailComposeViewController alloc] init];
This way the color scheme will be applied to the mail composer.
It may be returned back to defaults in mailComposeController:didFinishWithResult:
try this. worked for me.
MFMailComposeViewController* myailViewController = [[MFMailComposeViewController alloc] init];
// set other attributes of mailcomposer here.
myMailViewController.mailComposeDelegate = self;
[myMailViewController.navigationBar setTintColor:[UIColor whiteColor]];
[self presentViewController:myMmailViewController animated:YES completion:nil];
Swift 3 solution:
extension MFMailComposeViewController {
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
}
open override func viewDidLoad() {
super.viewDidLoad()
navigationBar.isTranslucent = false
navigationBar.isOpaque = false
navigationBar.barTintColor = UIColor.white
navigationBar.tintColor = UIColor.white
}
}
For iOS8:
NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];
[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];
Or
navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor yellowColor] forKey:UITextAttributeTextColor];
try this but one thing BarTintColor available only iOS7
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
This color is made translucent by default unless you set the translucent property to NO.
or try this link it will more helpful you
Changing MFMailComposeViewController's toolbar color
Try the following code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor blackColor]];
// Your usual code follows here ......
#SoftDesigner's answer:
As of iOS 9:
[UINavigationBar appearance].tintColor = yourFavoriteColor;
does not work on the MFMailComposeViewController.
The rest of the answer works (I used it), but as far as I can tell you're stuck with Apple's colors for the nav bar buttons.
Hope this saves someone else some angst.
First present the MFMailComposeViewController then change its tintColor
[self presentViewController:emailDialog animated:TRUE completion:nil];
[[UINavigationBar appearance] setBackgroundImage:nil
forBarPosition:UIBarPositionTopAttached
barMetrics:UIBarMetricsDefault];
I had a problem that prevented me from setting the background colour. Turns out I had some other code elsewhere setting the background image to [UIImage new].
The following code fixed it:
[[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:nil];

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

Remove Custom Nav Bar on Email Sheet

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.

Resources