59

i have a feedback button in my ios 7 application with MFMailComposeViewController. After the user click this button the mailcomposer open but the statusbar changed to black. Have anybody a idea what can i do?

i have this problem only with ios7. i customizing my app for ios7.

    MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
            mailController.mailComposeDelegate = self;

            [mailController setSubject:@"Feedback"];
            // Fill out the email body tex
            NSString *emailBody = [NSString stringWithFormat:@"testest"],
                                   [UIDevice currentDevice].model,
                                   [UIDevice currentDevice].systemVersion];
            [mailController setMessageBody:emailBody isHTML:NO];
            [mailController setToRecipients:[NSArray arrayWithObjects:@"support@test.com",nil]];

            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentModalViewController:mailController animated:YES];
}
Lion
  • 844
  • 1
  • 16
  • 43
NoBody
  • 719
  • 1
  • 6
  • 11

13 Answers13

143

Set the UIApplication statusBarStyle in the completion block of presentViewController for your MFMailComposeViewController. i.e.

    MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
    [self.navigationController presentViewController:mailVC animated:YES completion:^{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }];

You may also need to add and/or set "View controller-based status bar appearance" to NO in your Info.plist file.

Keller
  • 16,803
  • 8
  • 53
  • 71
  • 1
    If you make that change in your Info.plist, I don't think it matters where you do the setStatusBarStyle, as it becomes the global setting for the app. – mackinra Dec 18 '13 at 01:06
  • 1
    this shouldt be marked as the correct answer since preferredStatusBarStyle at this point on is useless – jfisk Jan 26 '14 at 21:07
  • 1
    @mackinra: Not exactly. I have it set on my plist and app delegate, but MailViewComposeViewController overrides it to black from white in my case. This solution has fixed it, as weird as it is. – micnguyen Jun 16 '15 at 12:00
  • I had to do `[self presentViewController:mailVC...` instead of `[self.navigationController...` – Islam Q. Aug 16 '15 at 13:33
  • In swift, I used `presentViewController(picker, animated: true, completion: {UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: false)})` where picker is `var picker = MFMailComposeViewController()` – Darren Aug 27 '15 at 09:06
  • 4
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; is deprecated now so I don't know what to do – MobileMon Dec 20 '16 at 14:43
  • Now that setStatusBarStyle is deprecated (since iOS 9 actually) what is the way to do it? Subclassing MFMailComposeViewController and overriding preferredStatusBarStyle does not work, any idea? – Zaphod Oct 03 '18 at 09:19
58

Try to add category to MFMailComposeViewController

EDIT: this solution works if "View controller-based status bar appearance" == YES

@implementation MFMailComposeViewController (IOS7_StatusBarStyle)

-(UIStatusBarStyle)preferredStatusBarStyle
{
   return UIStatusBarStyleLightContent;
}

-(UIViewController *)childViewControllerForStatusBarStyle
{
   return nil;
}

@end
Igor Palaguta
  • 3,490
  • 2
  • 18
  • 31
  • This does not work, because the view controller, and its status bar, are rendered by a different process entirely (MailCompositionService). This only influences the preview view controller that is animated while the external process loads. – Leo Natan Oct 11 '13 at 17:43
  • If you override MFMailComposeViewController class and implement just these two methods it works! – OemerA Oct 12 '13 at 10:24
  • 5
    This works! I have added exactly the same category and `childViewControllerForStatusBarStyle` does its job pretty well! Without it though nothing happens. – Yevhen Dubinin Dec 18 '13 at 00:52
  • 3
    This worked me too. Keller's answer will work if you want to make the setting global, but if you don't, this one does the trick. – mackinra Dec 18 '13 at 01:11
  • 2
    You shouldn't override existing methods in a category. This leads to unexpected behavior. You can read more about that here: https://www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html – NachoSoto May 14 '14 at 18:13
  • If I re-call this wont' work anymore because Apple patched categories so you can't override native functions to the class. – Oxcug Jan 03 '15 at 01:17
  • This solution still works when subclassing MFMailComposeViewController. – cbh2000 Jun 04 '15 at 22:31
  • This worked for me. iOS 8.4, Xcode 6.4, ARC enabled. Thanks Igor, here are a few helpful topics for anyone reviewing this solution... What is a category? https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/Category.html Review this article to learn how to add categories: http://stackoverflow.com/a/27555678/4018041 – serge-k Sep 05 '15 at 17:32
  • This works for me, running iOS 10.0 though I'm using a subclass rather than a category. – Chris Mar 17 '17 at 19:12
  • This works in iOS 11. I subclassed MFMailComposeViewController instead, to avoid global styling and implemented `preferredStatusBarStyle` (light content) and `childViewControllerForStatusBarStyle` (nil) and it works great. You must adjust info.plist as described above as well. – Daniel Saidi May 30 '18 at 06:49
25

Swift solution. Set View controller-based status bar appearance to YES

import UIKit
import MessageUI
import AddressBookUI

extension MFMailComposeViewController {
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .LightContent
    }

    override func childViewControllerForStatusBarStyle() -> UIViewController? {
        return nil
    }
}

extension ABPeoplePickerNavigationController {
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return .LightContent
    }

    override func childViewControllerForStatusBarStyle() -> UIViewController? {
        return nil
    }
}
SoftDesigner
  • 5,089
  • 2
  • 45
  • 45
  • 1
    This appears to be the correct answer with iOS 9. (Well, after removing the "public" keywords.) – Adam Kaump Nov 19 '15 at 22:29
  • 2
    If you want a broader solution, you can instead extend UINavigationController and override both preferredStatusBarStyle and childViewControllerForStatusBarStyle. That works for MFMailComposeViewController and the share sheets presented when sharing via text message and printing. (I haven't tested ABPeoplePickerNavigationController, since I'm not using that in my app.) – Jim Rhoades Dec 07 '15 at 22:23
  • Great idea, works perfectly. Tried a bunch of different swift approaches including the completionHanlder above, but the extension is definitely the way to go!!! – StuartM Apr 14 '16 at 11:48
  • 2
    In Swift 3: extension MFMailComposeViewController { override open var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } override open var childViewControllerForStatusBarStyle: UIViewController? { return nil } } – Barbara R Oct 11 '16 at 08:27
7

What did the trick for me was:

  • Subclass MFMailComposeViewController
  • Override the two methods as described in answer 6

    -(UIStatusBarStyle)preferredStatusBarStyle;

    -(UIViewController *)childViewControllerForStatusBarStyle;

  • Override viewDidLoad as follows:

    -(void)viewDidLoad {
    [super viewDidLoad];
    [self preferredStatusBarStyle];
    [self setNeedsStatusBarAppearanceUpdate];
    }

Lukasz 'Severiaan' Grela
  • 5,518
  • 4
  • 34
  • 69
7

Solution for Swift3

Add this to your ViewController:

extension MFMailComposeViewController {
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    open override var childViewControllerForStatusBarStyle: UIViewController? {
        return nil
    }
}

Set View controller-based status bar appearance >> YES as below:

enter image description here

Thanks to @SoftDesigner

Another cleaner solution that may not change other settings in your app. While presenting the Mail VC change the status bar in the completion block:

controller.present(mailComposeViewController, animated: true) {
            UIApplication.shared.statusBarStyle = .lightContent
        }
Tung Fam
  • 6,494
  • 4
  • 48
  • 55
5

Some times it will not update the status bar style properly. You should use

 [self setNeedsStatusBarAppearanceUpdate];

To say iOS to refresh the status bar style, manually. Hope someone would save some time on knowing it.

[self presentViewController:picker animated:YES completion:^{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
     [self setNeedsStatusBarAppearanceUpdate];
}];
2

The easiest swift 3 solution for me was:

extension MFMailComposeViewController {

    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        UIApplication.shared.statusBarStyle = .lightContent
    }
}
kalafun
  • 3,032
  • 5
  • 33
  • 47
1

None of above answers are work for me.

I have two issues.

  1. Black status bar
  2. transparent layer on title bar

enter image description here

Solution

  1. Black status - I remove all navigation bar customization

    // comment below line in AppDelegate

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg"] forBarMetrics:UIBarMetricsDefault];

  2. Transparent title bar - set navigationBarHidden = Yes for MFMailComposeViewController

    composeViewController.navigationBarHidden = YES;

Kirit Vaghela
  • 12,245
  • 4
  • 72
  • 79
1

It seems that initializing the MFMailComposeViewController UIApplication.shared.statusBarStyle will change to .default... so, saving the state before and setting it again after presentation solved the problem for me:

    // save the state, otherwise it will be changed
    let sbs = UIApplication.shared.statusBarStyle

    let mailComposerVC = MailComposerVC()
    mailComposerVC.navigationBar.barTintColor = UINavigationBar.appearance().barTintColor
    mailComposerVC.navigationBar.tintColor = UINavigationBar.appearance().tintColor
    mailComposerVC.navigationBar.barStyle = UINavigationBar.appearance().barStyle

    if MFMailComposeViewController.canSendMail() {
        APP_ROOT_VC?.present(mailComposerVC, animated: true, completion: {
            // reapply the saved state
            UIApplication.shared.statusBarStyle = sbs
        })
    }

    public class MailComposerVC: MFMailComposeViewController {

        public override var preferredStatusBarStyle: UIStatusBarStyle {
            return UIApplication.shared.statusBarStyle
        }
        public override var childViewControllerForStatusBarStyle : UIViewController? {
            return nil
        }
    }
Marco M
  • 1,125
  • 11
  • 12
0

iOS 7 introduces a method prefersStatusBarHidden, but it won't be so easy to use in this case. You may prefer to use the statusBarHidden property of UIApplication while the modal is presented.

Wain
  • 117,132
  • 14
  • 131
  • 151
  • thx 4 your answer. i test it with: [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone]; the same problem :( – NoBody Sep 22 '13 at 17:01
0
[self presentViewController:mailViewController animated:YES completion:^(void) { [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES]; }];
Praveen-K
  • 3,381
  • 1
  • 20
  • 30
0

In my case, I was using "view controller-based status bar appearance" and presenting a modal view controller with a custom segue transition and then presenting the MFMailComposeViewController from there. In this situation, by default, iOS only respects/uses the presenting or "root" view controller's preferredStatusBarStyle method.

So once I overrode childViewControllerForStatusBarStyle in my root view controller and preferredStatusBarStyle in my modal view controller, everything worked as expected... something like this:

// in RootViewController.m ...
- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.modalViewController;
}

// in ModalViewController.m ...
- (UIStatusBarStyle)preferredStatusBarStyle {
    if (self.mailController != nil)
        return UIStatusBarStyleDefault;
    return UIStatusBarStyleLightContent;
}
taber
  • 3,096
  • 3
  • 43
  • 68
0

I am building an application in iOS8 and have had issues with the status bar with all native functions such as the mail composer, the camera, etc.. The following will solve your issues:

Put the following in your plist file

  <key>UIStatusBarHidden</key>
  <false/>
  <key>UIViewControllerBasedStatusBarAppearance</key>
  <false/>

If you are using the add row feature in storyboard, the UIViewControllerBasedStatusBarAppearance is not an option. Also when adding a row it asks for BOOLEAN (YES/NO). It cannot be a NO string in the source code it must be a false boolean. Open the plist as source code instead and add the above rows. Remove your old attempts. You will now be able to successfully apply the code snippets given in so many incomplete answers found on the net.

You can now add global changes in the app delegate file and/or overrides in the controllers themselves. Without the above being in place all the stack overflow code I have tried has failed when using a native function. Now all is working perfectly.

As a test, replace any calls to any onboard "completion" calls with

    completion:^{[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];}