iOS 7状态栏中的MFMailComposeViewController是黑色的

时间:2013-09-22 15:19:26

标签: ios ios7 mfmailcomposeviewcontroller

我在带有MFMailComposeViewController的ios 7应用程序中有一个反馈按钮。用户单击此按钮后,mailcomposer将打开,但状态栏将更改为黑色。有谁知道我该怎么办?

我只有ios7才有这个问题。我为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];
}

13 个答案:

答案 0 :(得分:143)

在presentViewController的完成块中为MFMailComposeViewController设置UIApplication statusBarStyle。即。

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

您可能还需要在Info.plist文件中添加和/或设置“查看基于控制器的状态栏外观”为NO。

答案 1 :(得分:58)

尝试将类别添加到MFMailComposeViewController

编辑:如果“查看基于控制器的状态栏外观”== YES

,此解决方案有效
@implementation MFMailComposeViewController (IOS7_StatusBarStyle)

-(UIStatusBarStyle)preferredStatusBarStyle
{
   return UIStatusBarStyleLightContent;
}

-(UIViewController *)childViewControllerForStatusBarStyle
{
   return nil;
}

@end

答案 2 :(得分:23)

快速解决方案。 将View controller-based status bar appearance设为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
    }
}

答案 3 :(得分:7)

我的诀窍是:

  • 子类MFMailComposeViewController
  • 按照答案6

    中的描述覆盖两种方法

    -(UIStatusBarStyle)preferredStatusBarStyle;

    -(UIViewController *)childViewControllerForStatusBarStyle;

  • 覆盖viewDidLoad,如下所示:

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

答案 4 :(得分:7)

解决 Swift3

将此添加到您的ViewController:

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

设置View controller-based status bar appearance>>是,如下所示:

enter image description here

感谢@SoftDesigner

另一个更清洁解决方案,可能不会更改您应用中的其他设置。在呈现Mail VC时,更改完成块中的状态栏:

controller.present(mailComposeViewController, animated: true) {
            UIApplication.shared.statusBarStyle = .lightContent
        }

答案 5 :(得分:5)

有时它不会正确更新状态栏样式。你应该使用

 [self setNeedsStatusBarAppearanceUpdate];

要说iOS要手动刷新状态栏样式。希望有人能节省一些时间来了解它。

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

答案 6 :(得分:2)

对我来说,最简单的 swift 3 解决方案是:

extension MFMailComposeViewController {

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

        UIApplication.shared.statusBarStyle = .lightContent
    }
}

答案 7 :(得分:1)

以上答案都不适合我。

我有两个问题。

  1. 黑色状态栏
  2. 标题栏上的透明图层
  3. enter image description here

    解决方案

    1. 黑色状态 - 我删除了所有导航栏自定义

      //在AppDelegate的下面注释

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

    2. 透明标题栏 - 为MFMailComposeViewController设置navigationBarHidden = Yes

      composeViewController.navigationBarHidden = YES;

答案 8 :(得分:1)

似乎初始化MFMailComposeViewController UIApplication.shared.statusBarStyle将更改为.default ...所以,之前保存状态并在演示后再次设置它解决了我的问题:

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

答案 9 :(得分:0)

iOS 7引入了一种方法prefersStatusBarHidden,但在这种情况下使用它并不容易。在呈现模式时,您可能更喜欢使用statusBarHidden UIApplication属性。

答案 10 :(得分:0)

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

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

答案 11 :(得分:0)

在我的情况下,我使用“基于视图控制器的状态栏外观”并呈现具有自定义segue转换的模态视图控制器,然后然后从那里呈现MFMailComposeViewController。在这种情况下,默认情况下,iOS仅尊重/使用呈现或“root”视图控制器的preferredStatusBarStyle方法。

所以,一旦我在我的模态视图控制器中的根视图控制器 childViewControllerForStatusBarStyle中覆盖preferredStatusBarStyle,一切都按预期工作......就像这样:

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

// in ModalViewController.m ...
- (UIStatusBarStyle)preferredStatusBarStyle {
    if (self.mailController != nil)
        return UIStatusBarStyleDefault;
    return UIStatusBarStyleLightContent;
}

答案 12 :(得分:0)

我正在iOS8中构建一个应用程序,状态栏存在问题,包括邮件编辑器,相机等所有本机功能。以下内容将解决您的问题:

将以下内容放入plist文件

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

如果您在故事板中使用添加行功能,则UIViewControllerBasedStatusBarAppearance不是一个选项。此外,当添加一行时,它要求BOOLEAN(是/否)。它不能是源代码中的NO字符串,它必须是false布尔值。打开plist作为源代码,然后添加上面的行。删除旧的尝试。您现在可以成功应用在网上找到的许多不完整答案中给出的代码片段。

您现在可以在应用委托文件中添加全局更改和/或在控制器本身中覆盖。如果没有上述原因,我尝试过的所有堆栈溢出代码在使用本机函数时都会失败。现在一切都很完美。

作为测试,用

替换对任何板载“完成”调用的任何调用
    completion:^{[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];}