Reputation: 1304
I have a simple app which is a UITableViewController
. The app uses custom themes which applies a theme to the background and the UINavigationBar
. From this view, I have a UINavigationBarItem
(created in code) that brings up the email sheet with MFMailComposerViewController
.
I'm finding some weird behaviour with the custom themed UINavigationBar
and so I just want to remove the image and have a white UINavigationBar with black buttons (cancel, send, title).
I have the code below:
if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:@"PDF"];
[mailer addAttachmentData:data mimeType:@"application/pdf" fileName:fileName];
NSString *emailBody = @"Please find attached PDF";
[mailer setMessageBody:emailBody isHTML:NO];
[self presentViewController:mailer animated:YES completion:^{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
mailer.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor blackColor]};
[[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
[[mailer navigationBar] setTintColor:[UIColor blackColor]];
}];
}
So I am actually removing the image there. The issue is that it works, but it doesn't actually make the buttons black as they should be. Also, in some instances, there are occurrences where the custom themed UINavigationBar
image shows through. I want this to never happen.
I even went as far as creating a new class which was a subclass of MFMailComposer
and setting the "theme" of the UINavigationBar
to be a white created bar, but that still brought about the images sometimes.
What I want is to ensure the UINavigationBar
never shows an image and always has black buttons for the Cancel, Title and Send.
Any thoughts would be really appreciated.
Update
With removing the setBarTintColor
and setTintColor
code above, I have noticed that the first time I invoke the MFMailComposeViewController
, the theme is there in the UINavigationBar
, but if I close that view, and re-open it, then the image is removed and the UINavigationBar
is white. How can I get this to show up as white always, without a theme? I have also removed the code that removes the images to outside of the completion block, but that didn't make a difference.
Upvotes: 0
Views: 261
Reputation: 801
To fill UINavigationBar with color you can use backgroundImage property with blank shadow image:
[[UINavigationBar appearance] setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
-imageWithColor is an additional method for creating UIImage with selected color:
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Then set black text color and black status bar so it will be visible:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]}];
And don't forget that these properties will be applied to every navigation bar. Hope it helps! :)
Upvotes: 2