简体   繁体   English

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

[英]MFMailComposeViewController in iOS 7 statusbar are black

i have a feedback button in my ios 7 application with MFMailComposeViewController. 我在带有MFMailComposeViewController的ios 7应用程序中有一个反馈按钮。 After the user click this button the mailcomposer open but the statusbar changed to black. 用户单击此按钮后,mailcomposer将打开,但状态栏将更改为黑色。 Have anybody a idea what can i do? 有谁知道我该怎么办?

i have this problem only with ios7. 我只有ios7才有这个问题。 i customizing my app for 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];
}

Set the UIApplication statusBarStyle in the completion block of presentViewController for your MFMailComposeViewController. 在presentViewController的完成块中为MFMailComposeViewController设置UIApplication statusBarStyle。 ie

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

Try to add category to MFMailComposeViewController 尝试将类别添加到MFMailComposeViewController

EDIT: this solution works if "View controller-based status bar appearance" == YES 编辑:如果“查看基于控制器的状态栏外观”== 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 View controller-based status bar appearanceYES

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

What did the trick for me was: 对我来说诀窍是:

  • Subclass MFMailComposeViewController 子类MFMailComposeViewController
  • Override the two methods as described in answer 6 覆盖答案6中描述的两种方法

    -(UIStatusBarStyle)preferredStatusBarStyle;

    -(UIViewController *)childViewControllerForStatusBarStyle;

  • Override viewDidLoad as follows: 覆盖viewDidLoad如下:

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

Solution for Swift3 Swift3的解决方案

Add this to your ViewController: 将其添加到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: 设置View controller-based status bar appearance >> YES如下:

在此输入图像描述

Thanks to @SoftDesigner 感谢@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: 在呈现Mail VC时,更改完成块中的状态栏:

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. 要说iOS要手动刷新状态栏样式。 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 //在AppDelegate中注释以下行

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

  2. Transparent title bar - set navigationBarHidden = Yes for MFMailComposeViewController 透明标题栏 - 为MFMailComposeViewController设置navigationBarHidden = Yes

    composeViewController.navigationBarHidden = YES; 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: 似乎初始化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
        }
    }

iOS 7 introduces a method prefersStatusBarHidden , but it won't be so easy to use in this case. iOS 7引入了一种方法prefersStatusBarHidden ,但在这种情况下它不会那么容易使用。 You may prefer to use the statusBarHidden property of UIApplication while the modal is presented. 在呈现模式时,您可能更喜欢使用UIApplicationstatusBarHidden属性。

[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. 在我的情况下,我使用“基于视图控制器的状态栏外观”并呈现具有自定义segue转换的模态视图控制器, 然后从那里呈现MFMailComposeViewController。 In this situation, by default, iOS only respects/uses the presenting or "root" view controller's preferredStatusBarStyle method. 在这种情况下,默认情况下,iOS仅尊重/使用呈现或“根”视图控制器的preferredStatusBarStyle方法。

So once I overrode childViewControllerForStatusBarStyle in my root view controller and preferredStatusBarStyle in my modal view controller, everything worked as expected... something like this: 所以,一旦我在我的模态视图控制器中覆盖了我的根视图控制器中的childViewControllerForStatusBarStyle preferredStatusBarStyle ,一切都按预期工作......这样的事情:

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

Put the following in your plist file 将以下内容放在plist文件中

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

If you are using the add row feature in storyboard, the UIViewControllerBasedStatusBarAppearance is not an option. 如果您在故事板中使用添加行功能,则UIViewControllerBasedStatusBarAppearance不是一个选项。 Also when adding a row it asks for BOOLEAN (YES/NO). 此外,当添加一行时,它要求BOOLEAN(是/否)。 It cannot be a NO string in the source code it must be a false boolean. 它不能是源代码中的NO字符串,它必须是false布尔值。 Open the plist as source code instead and add the above rows. 打开plist作为源代码,然后添加上面的行。 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];}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM