문제

I am working with my app that calls up the MFMailComposeViewController from a particular UITableView when a navigationBarButton has been tapped.

Because I have themes in my app (a choice of 12), I first thought it would be good to have the MFMailComposeViewController come up with the same theme and I'm creating this directly from code and not from the Storyboard.

While testing my app, I'm noticing that the NavigationBar image does not remain consistent each and every time this view is launched. Within Storyboard, I have two navigation bar items on the right side which is implemented by having a UIView with a UIToolbar embedded in that view and finally the two navigation bar button items in that UIToolbar. This allows me to have both next to each other.

With this in mind, I've noticed a pattern. If I click on the Email icon, the MFMailComposeViewController that comes up has a NavigationBar that takes the appropriate image. If I tap the second icon in the navigation bar (Plus icon) and then go and tap the Email icon again, the UINavigationBar is white and therefore it doesn't load the image. I've put in NSLogs and the code is getting called appropriately for setting the UINavigationBar, but this is the pattern; click on the other UINavigationBar Button item first and then click on the Email and the UINavigationBar does not show the UIImage. Here's the code:

- (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];
        }];
    }
}

Problem

So, to reiterate the issue: when I select and call up this MFMailComposeViewController in the app by tapping the navigation bar button, the UIImage is shown if I click on the Email button first, but if I click on the Plus button and then the email, the uinavigationbar image is not shown on the MFMailComposeViewController.

What I want here is for the UINavigationBar to always take the selected theme's UINavigationBar image.

The AppDelegate already sets the code for the selected theme and the UINavigationBar's image, but it seems to not get applied here. Here's a little snippet:

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

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

What can I do to ensure the MFMailComposeViewController's UINavigationBar image remains that of the selected theme?

Every other view is perfectly fine; it's just this one.

The weird thing is the fact that elsewhere in my app, I call another MFMailComposeViewController from a UITableViewCell with the exact same code (just changing the subject, etc) and that keeps the Image of the UINavigationBar every time; it's just from here with the UINavigationBar Button that the image does not remain.

Update: I've found some steps to reproduce this. In the view where I have the UINavigationBar button that calls the MFMailComposerViewController, I also have a second NavigationBar item that allows a user to add an entry from here. That is created in Storyboard with a UIView and a UIToolbar embedded within that view and the two NavigationBar items (Email and Add) inside that toolbar. If I click on the mail icon, it appears consistently with the image. However, if I tap the plus button in the nav bar, cancel that and then click on the email icon, it doesn't display the UINavigationBar image when the MFMailComposer comes up.

Any guidance would really be appreciated.

도움이 되었습니까?

해결책

Your question is really lengthy, however answer are pretty simple.

Please refer these Stack Over flow post that talks about changing the MFMailComposeViewController 's navigationBar & tints. You can try it in your case. however your question is not a duplicate, So I am referring the related posts here.

Link 1

Link 2

UPDATED SECTION

  • The idea is to control the UINavigationBar appearance in viewWillAppear method of any viewController.
  • Here is the Video of this.
  • Here is the screen shots. enter image description here
  • Finally if you want the sample code, you can check it. There is no use of Singleton. You can modify it accordingly.

다른 팁

//Try

-(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];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top