如何在MFMailcomposer视图中显示默认导航栏,而不是在ios中显示自定义导航栏

时间:2013-12-19 17:30:38

标签: ios uinavigationcontroller custom-controls objective-c-blocks

我有一个自定义导航栏,其图像对所有视图都是通用的。我也有一个MFMailComposer视图,但是我没有在这里找到带有发送和取消按钮的默认导航栏。我试图从导航栏中删除图像但是没工作。这就是我试过的:

-(void)mailShare:(id)sender{
 [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
 Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));

    if (mailClass != nil) {
        //[self displayMailComposerSheet];
        // We must always check whether the current device is configured for sending emails
        if ([mailClass canSendMail])
        {
            [self applyComposerInterfaceApperance];


        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Device not configured to send mail" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            alert = nil;
            return;
        }
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Device not configured to send mail" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        alert = nil;
        return;
    }

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
  // [ picker.navigationBar setTintColor:[UIColor greenColor]];

    [picker setSubject:@"Try this Pack from FORCE PACKS"];


    CGRect rect = CGRectMake(0, 44, 320, 440);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];


    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


    // Attach an image to the email
    NSData *myData =  UIImagePNGRepresentation(img);
    [picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"rainy"];

    // Fill out the email body text
    NSString *emailBody = @"I'm on the road to recovery! Check out my latest Exercise Log from FORCE Packs";
    [picker setMessageBody:emailBody isHTML:NO];

    [self presentViewController:picker animated:YES completion:nil];

    myData = nil;



}

2 个答案:

答案 0 :(得分:1)

我在我的应用中遇到了同样的问题。我删除了使用UIAppearance协议设置导航栏图像的代码。相反,我在每个需要自定义导航栏的视图控制器中为UINavigationBar设置图像。

我创建了一个UIViewController类,其中包含我在每个UIViewController的-viewDidLoad中调用的方法来自定义导航栏。

在UIViewController + Appearance.h中

-(void)changeAppearance;

在UIViewController + Appearance.m

-(void)changeAppearance{
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage imageNamed:@"Header"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6)] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeFont: [UIFont fontWithName:@"CooperBlack" size:18], UITextAttributeTextColor: [UIColor colorWithRed:57.0f/255.0f green:132.0f/225.0f blue:168.0f/225.0f alpha:1],UITextAttributeTextShadowColor: [UIColor clearColor]}];

    [self.navigationController.navigationBar setShadowImage:[[UIImage imageNamed:@"Header-shadow"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6)]];

    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]]];
} 

首先在视图控制器的UIViewController+Appearance.h文件中导入.m,然后在视图控制器的-changeAppearance中调用-viewDidLoad方法,如:

[self changeAppearance];

同时删除您当前用于自定义导航栏的所有通话,例如:[[UINavigationBar appearance] blahBlahBlah]

我希望这会有所帮助。

答案 1 :(得分:0)

我明白了!这是代码:

[UINavigationBar appearance] setBackgroundImage:nil
 forBarMetrics:UIBarMetricsDefault]; MFMailComposeViewController
 *emailVC = [[MFMailComposeViewController alloc] init]; //the rest of the implementation goes here... [self presentViewController:emailVC
 animated:YES completion:nil]; Then, I set the nav bar appearance back
 to normal here:

 - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
 {
     [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"Nav Bar.png"] forBarMetrics:UIBarMetricsDefault];
     [self dismissViewControllerAnimated:YES completion:nil];
 }