【问题标题】:How to show the default navigation bar in MFMailcomposer view rather than custom nav bar in ios如何在 MFMailcomposer 视图中显示默认导航栏,而不是在 ios 中显示自定义导航栏
【发布时间】:2013-12-19 17:30:38
【问题描述】:

我有一个自定义导航栏,其中包含所有视图的通用图像。此外,我还有一个 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;



}

【问题讨论】:

  • 能否添加用于自定义导航栏的代码?
  • @Clever 错误肯定。这是代码:[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBar.png"] forBarMetrics:UIBarMetricsDefault];

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


【解决方案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"]]];
} 

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

[self changeAppearance];

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

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我想通了!代码如下:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多