簡體   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