MFMailComposeViewController in iOS 7 statusbar are black

与世无争的帅哥 提交于 2019-11-27 18:40:22

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.

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

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
    }
}
user1769627

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

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:

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
        }

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

The easiest swift 3 solution for me was:

extension MFMailComposeViewController {

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

        UIApplication.shared.statusBarStyle = .lightContent
    }
}

None of above answers are work for me.

I have two issues.

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

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;

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
        }
    }

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.

[self presentViewController:mailViewController animated:YES completion:^(void) { [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES]; }];

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

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];}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!