MFMailComposeViewController的UINavigationBar不一致地显示正确的图像/颜色

时间:2014-04-07 08:03:37

标签: ios iphone uiimage uinavigationbar mfmailcomposeviewcontroller

我正在使用我的应用,当点按MFMailComposeViewController时,该应用会从特定UITableView调出navigationBarButton

因为我的应用程序中有主题(12种选择),我首先认为让MFMailComposeViewController提出相同的主题并且我直接从代码和不是来自故事板。

在测试我的应用时,我注意到NavigationBar图像在每次启动此视图时都不会保持一致。在Storyboard中,我在右侧有两个导航栏项目,通过在该视图中嵌入UIToolbar的UIVool实现,最后是UIToolbar中的两个导航栏按钮项目。这使我可以彼此相邻。

考虑到这一点,我注意到了一种模式。如果单击“电子邮件”图标,则出现的MFMailComposeViewController会有一个带有相应图像的NavigationBar。如果我点击导航栏中的第二个图标(加号图标)然后再次点击电子邮件图标,则UINavigationBar为白色,因此它不会加载图像。我已经放入了NSLogs,并且正在调用代码来设置UINavigationBar,但这是模式;首先单击另一个UINavigationBar Button项,然后单击Email,UINavigationBar不显示UIImage。 这是代码:

- (IBAction)savePDFAndEmail:(id)sender
{
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        mailer.mailComposeDelegate = self;
        [mailer setSubject:@"An email"];

        NSString *emailBody = @"Hello";
        [mailer setMessageBody:emailBody isHTML:NO];
        mailer.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
        [[mailer navigationBar] setTintColor:[UIColor whiteColor]];

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

问题

所以,重申一下这个问题:当我通过点击导航栏按钮选择并在应用程序中调用此MFMailComposeViewController时,如果我先单击“电子邮件”按钮,则显示UIImage,但如果我点击加号按钮然后是电子邮件,uinavigationbar图像未显示在MFMailComposeViewController上。

我想要的是UINavigationBar始终采用所选主题的UINavigationBar图像。

AppDelegate已经为所选主题和UINavigationBar's图像设置了代码,但似乎没有在此处应用。这是一个小片段:

else if ([self.selectedTheme isEqualToString:@"Simplicity"])
{
    UIImage *navBackgroundImage = [UIImage imageNamed:@"GreenishNav.png"];

    [[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];

我可以做些什么来确保MFMailComposeViewController's UINavigationBar图片仍然是所选主题的图像?

其他每一种观点都很好;它就是这个。

奇怪的是,在我的应用程序的其他地方,我使用完全相同的代码(仅更改主题等)从MFMailComposeViewController调用另一个UITableViewCell并保留了每次UINavigationBar;它只是从这里使用UINavigationBar按钮,图像不会保留。

更新:我找到了重现此步骤的一些步骤。在我拥有调用MFMailComposerViewController的UINavigationBar按钮的视图中,我还有一个允许用户从此处添加条目的第二个NavigationBar项。这是在Storyboard中创建的,其中嵌入了该视图中的UIView和UIToolbar以及该工具栏中的两个NavigationBar项(Email和Add)。如果我单击邮件图标,它将与图像一致。但是,如果我点击导航栏中的加号按钮,取消该按钮,然后单击电子邮件图标,当MFMailComposer出现时,它不会显示UINavigationBar图像。

任何指导都会非常感激。

2 个答案:

答案 0 :(得分:1)

你的问题非常冗长,但答案很简单。

请参阅这些Stack Over流程帖子,其中讨论了如何更改MFMailComposeViewController的navigationBar&色调。您可以在您的情况下尝试。但是你的问题不重复,所以我在这里提到相关的帖子。

Link 1

Link 2

更新部分

  • 我们的想法是控制任何viewController的UINavigationBar方法中的viewWillAppear外观。
  • 以下是Video
  • 这是截屏。 enter image description here
  • 最后,如果您想要样本code,可以查看它。单身人士没有用。您可以相应地修改它。

答案 1 :(得分:0)

//尝试

-(void)displayMailComposerSheet
{
    if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
    {

    }
    else
    {
        [[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    }


    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;

    if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
    {

    }
    else
    {
        picker.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:UITextAttributeTextColor];
    }

    NSLog(@"%@",Data_Dict);

    [picker setSubject:[NSString stringWithFormat:@"%@",[Data_Dict objectForKey:@"short_name"]]];

    // Set up recipients
    NSArray *toRecipients = [NSArray arrayWithObject:@""];

    [picker setToRecipients:toRecipients];

    //  // Attach an image to the email
    //  NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"jpg"];
    //  NSData *myData = [NSData dataWithContentsOfFile:path];
    //  [picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy"];

    // Fill out the email body text
    NSString *emailBody = str_sms;
    [picker setMessageBody:emailBody isHTML:NO];

    [self presentModalViewController:picker animated:YES];
    [picker release];
}



#pragma mark -
#pragma mark Dismiss Mail/SMS view controller
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    ////NSLog(@"%u",result);


    if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
    {

    }
    else
    {

        [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];

        // Uncomment to change the color of back button
        [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

        // Uncomment to assign a custom backgroung image
        [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg_ios7.png"] forBarMetrics:UIBarMetricsDefault];

        // Uncomment to change the back indicator image

        [[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@""]];
        [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@""]];

        // Uncomment to change the font style of the title

        NSShadow *shadow = [[NSShadow alloc] init];
        shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
        shadow.shadowOffset = CGSizeMake(0, 1);

        [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,shadow, NSShadowAttributeName,[UIFont fontWithName:@"ArialMT" size:18.0], NSFontAttributeName, nil]];

        CGFloat verticalOffset = -4;
        [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];
    }


    if(result == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Result" message:@"Mail sending canceled" delegate:Nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    else if(result == 1)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Result" message:@"Result: Mail saved" delegate:Nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    else if(result == 2)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Result" message:@"Mail sent" delegate:Nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    else if(result == 3)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Result" message:@"Mail sending failed" delegate:Nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Result" message:@"Mail not sent" delegate:Nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    [self dismissModalViewControllerAnimated:YES];
}