Ios 自定义从UITextView打开的自动MFMailComposeViewController
我有一个简单的Ios 自定义从UITextView打开的自动MFMailComposeViewController,ios,uitextview,mfmailcomposeviewcontroller,Ios,Uitextview,Mfmailcomposeviewcontroller,我有一个简单的UITextView,里面有一个电子邮件链接。textview可选择并检测链接。这样,您可以单击电子邮件,它会打开一个MFMailComposeViewController视图控制器 但是,我在应用程序发布时进行了一些定制: [[UINavigationBar appearance] setBarTintColor: myGreyColor]; [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundCo
UITextView
,里面有一个电子邮件链接。textview可选择并检测链接。这样,您可以单击电子邮件,它会打开一个MFMailComposeViewController
视图控制器
但是,我在应用程序发布时进行了一些定制:
[[UINavigationBar appearance] setBarTintColor: myGreyColor];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: myFont}];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
这样,所有导航栏都是灰色的,带有白色文本和白色按钮,标题有自定义字体
我的问题是,所有这些都不适用于邮件生成器:栏是灰色的,标题是白色的,但字体是默认的helvetica neue,按钮是默认的蓝色。而且,状态栏是黑色的,即使my Info.plist显示UIStatusBarStyleLightContent
,并且查看基于控制器的状态栏外观
设置为否
当我手动调用MFMailComposeViewController时,我知道如何自定义它,但在这里它会自动弹出。如何将我的样式应用于它?编辑
定制MFMailComposeViewController
的外观是一个非常糟糕的主意,很可能会让你的应用被苹果拒绝。只有当您不打算向苹果提交应用程序时,才应使用以下解决方案
看来我解决了,多亏了(再次…)。以下是完整的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
[…] // Initializations
// Link detection
[_textView.attributedText addAttribute:NSLinkAttributeName value:@"mail://contact" range:[[content string] rangeOfString:@"contact@mymail.com"]];
_textView.delegate = self;
}
// Handle the link tap yourself
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
if ([[URL scheme] isEqualToString:@"mail"]) {
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[mailVC setToRecipients:@[@"contact@ mymail.com"]];
[mailVC setSubject:@"About QuickReminder for iOS"];
mailVC.mailComposeDelegate = self;
// Re-set the styling
[mailVC.navigationBar setBarTintColor:myGreyColor];
[mailVC.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: myFont}];
[mailVC.navigationBar setTintColor:[UIColor whiteColor]];
[self presentViewController:mailVC animated:YES completion:^{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}];
return NO;
}
return YES;
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog(@"Result: saved");
break;
case MFMailComposeResultSent:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result" message:@"Mail Sent Successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
break;
case MFMailComposeResultFailed:
NSLog(@"Result: failed");
break;
default:
NSLog(@"Result: not sent");
break;
}
[controller dismissViewControllerAnimated:YES completion:nil];
}
然后,您应该执行以下外观方法,而不是修改整个应用程序导航栏:-
[[UINavigationBar appearanceWhenContainedIn:<#(__unsafe_unretained Class<UIAppearanceContainer> *), ...#>, nil]setTintColor:[UIColor greenColor]];
[[UINavigationBar在包含时的外观:,nil]setTintColor:[UIColor greenColor]];
因为您的MFMailComposeViewController打开到应用程序中,并且您正在修改整个应用程序的导航栏,所以MFMailComposeViewController的导航栏正在修改。
使用上述外观方法,您可以修改所选类或父类,通过这些类可以修改派生类,但这不会修改MFMailComposeViewController,因为它不是父类的一部分。Swift 3:
extension MFMailComposeViewController {
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
}
open override func viewDidLoad() {
super.viewDidLoad()
navigationBar.isTranslucent = false
navigationBar.isOpaque = false
navigationBar.barTintColor = UIColor.white
navigationBar.tintColor = UIColor.white
}
}
@rdurandI在MFMailComposeViewController导航颜色方面也遇到了同样的问题。你能给我同样的建议吗?@APG:我写了一个答案。最好的方法是手动构建MFMailComposeViewController并直接对其进行自定义。然后在UITextView中截取链接并亲自展示邮件视图控制器。@APG:我更新了我的答案。经过一些研究,定制邮件生成器似乎是一个非常糟糕的主意,它会让你的应用被苹果拒绝。当您更改外观时,应使用appearanceWhenContainedIn以您自己的视图控制器为目标,并让mail composer单独工作。当将电子邮件键入NSString并设置为UITextview的文本(即不使用属性字符串)时,[URL方案]应为“mailto”