gpt4 book ai didi

ios - 在应用程序中显示带有自定义 UINavigationBar 主题的 MFMailComposer 的白色 UINavigationBar

转载 作者:行者123 更新时间:2023-11-28 21:55:32 26 4
gpt4 key购买 nike

我有一个简单的应用程序,它是一个 UITableViewController。该应用程序使用自定义主题,将主题应用于背景和 UINavigationBar。从这个角度来看,我有一个 UINavigationBarItem(在代码中创建),它使用 MFMailComposerViewController 调出电子邮件表。

我发现自定义主题 UINavigationBar 有一些奇怪的行为,所以我只想删除图像并使用带有黑色按钮(取消、发送、标题)的白色 UINavigationBar。

我有以下代码:

if ([MFMailComposeViewController canSendMail])
{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:@"PDF"];
[mailer addAttachmentData:data mimeType:@"application/pdf" fileName:fileName];
NSString *emailBody = @"Please find attached PDF";
[mailer setMessageBody:emailBody isHTML:NO];

[self presentViewController:mailer animated:YES completion:^{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
mailer.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor blackColor]};
[[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
[[mailer navigationBar] setTintColor:[UIColor blackColor]];

}];
}

所以我实际上是在删除那里的图像。问题是它可以工作,但实际上并没有使按钮变黑。此外,在某些情况下,会出现自定义主题 UINavigationBar 图像显示的情况。我希望这永远不会发生。

我什至创建了一个新类,它是 MFMailComposer 的子类,并将 UINavigationBar 的“主题”设置为白色创建的栏,但是有时仍然会带来图像。

我想要的是确保 UINavigationBar 从不显示图像,并且始终具有用于取消、标题和发送的黑色按钮。

如有任何想法,我们将不胜感激。

更新

删除上面的 setBarTintColorsetTintColor 代码后,我注意到我第一次调用 MFMailComposeViewController 时,主题就在UINavigationBar,但如果我关闭该 View 并重新打开它,则图像将被删除并且 UINavigationBar 为白色。我怎样才能让它在没有主题的情况下始终显示为白色?我还删除了将图像删除到完成 block 之外的代码,但这并没有什么不同。

最佳答案

要用颜色填充 UINavigationBar,您可以使用带有空白阴影图像的 backgroundImage 属性:

[[UINavigationBar appearance] setBackgroundImage:[self imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

-imageWithColor 是另一种创建具有选定颜色的 UIImage 的方法:

- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);

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

return image;
}

然后设置黑色文本颜色和黑色状态栏,使其可见:

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blackColor]}];

不要忘记这些属性将应用于每个导航栏。希望能帮助到你! :)

关于ios - 在应用程序中显示带有自定义 UINavigationBar 主题的 MFMailComposer 的白色 UINavigationBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26731552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com