繁体   English   中英

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

[英]MFMailComposeViewController in iOS 7 statusbar are black

我在带有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];
}

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

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

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

尝试将类别添加到MFMailComposeViewController

编辑:如果“查看基于控制器的状态栏外观”== YES,此解决方案有效

@implementation MFMailComposeViewController (IOS7_StatusBarStyle)

-(UIStatusBarStyle)preferredStatusBarStyle
{
   return UIStatusBarStyleLightContent;
}

-(UIViewController *)childViewControllerForStatusBarStyle
{
   return nil;
}

@end

迅捷解决方案。 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
    }
}

对我来说诀窍是:

  • 子类MFMailComposeViewController
  • 覆盖答案6中描述的两种方法

    -(UIStatusBarStyle)preferredStatusBarStyle;

    -(UIViewController *)childViewControllerForStatusBarStyle;

  • 覆盖viewDidLoad如下:

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

Swift3的解决方案

将其添加到ViewController:

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

设置View controller-based status bar appearance >> YES如下:

在此输入图像描述

感谢@SoftDesigner

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

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

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

 [self setNeedsStatusBarAppearanceUpdate];

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

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

对我来说,最简单的快速解决方案是:

extension MFMailComposeViewController {

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

        UIApplication.shared.statusBarStyle = .lightContent
    }
}

以上答案都不适合我。

我有两个问题。

  1. 黑色状态栏
  2. 标题栏上的透明图层

在此输入图像描述

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

    //在AppDelegate中注释以下行

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

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

    composeViewController.navigationBarHidden = YES;

似乎初始化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引入了一种方法prefersStatusBarHidden ,但在这种情况下它不会那么容易使用。 在呈现模式时,您可能更喜欢使用UIApplicationstatusBarHidden属性。

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

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

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

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

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

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

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

将以下内容放在plist文件中

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

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

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

作为测试,将任何对板载“完成”呼叫的任何呼叫替换为

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

暂无
暂无

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

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