MFMailComposeViewController navigationBar 覆盖在模拟器中有效,但在设备中无效

MFMailComposeViewController navigationBar override works in Simulator but not device

默认情况下,我的邮件编写器会选择它的呈现视图控制器的背景图像。所以我有逻辑来禁用那个背景,它在模拟器中像冠军一样工作但在物理设备上不是(或者至少 iPhone 4S。两者都使用 iOS 8.1

- (void)composeEmail
{
if ([MFMailComposeViewController canSendMail])
{

        self.mailComposer = [[MFMailComposeViewController alloc] init];
        self.mailComposer.mailComposeDelegate = self;

        // Disable navbar styling in presenting VC
        [[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];


        NSString *emailTitle = @"Subject";
        NSString *messageBody = @"Body";

        //configure mail message

        [self.mailComposer setSubject:emailTitle];
        [self.mailComposer setMessageBody:messageBody isHTML:NO];


        // Present mail view controller on screen
        [self.callingController presentViewController:self.mailComposer animated:YES completion:^{
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

        }];
    }
} else {
    // alert here
}
}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
    case MFMailComposeResultCancelled:
        NSLog(@"Mail cancelled");
        break;
    case MFMailComposeResultSaved:
        NSLog(@"Mail saved");
        break;
    case MFMailComposeResultSent:
        NSLog(@"Mail sent");
        break;
    case MFMailComposeResultFailed:
        NSLog(@"Mail sent failure: %@", [error localizedDescription]);
        break;
    default:
        break;
}

// Enable navbar styling in presenting VC
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"logo.png"] forBarMetrics:UIBarMetricsDefault];

// Close the Mail Interface
[self.callingController dismissViewControllerAnimated:YES completion:NULL];
}

您的代码有两处更改:

  1. 在撰写电子邮件方法中使用下行

    [self.mailComposer.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    
  2. 在 didFinishWithResult 方法中使用下面一行。

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Happy-Rose-Day.png"] forBarMetrics:UIBarMetricsDefault];
    

我认为最后一点可能不需要,因为您的代码也可以正常工作([UINavigationBar appearance])

事实证明,解决方案既简单又不明显。修改 UINavigationBar 的命令必须在分配邮件编辑器之前发出。

[[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
self.mailComposer = [[MFMailComposeViewController alloc] init];