【问题标题】:Customizing automatic MFMailComposeViewController opened from UITextView自定义从 UITextView 打开的自动 MFMailComposeViewController
【发布时间】:2014-03-03 10:55:47
【问题描述】:

我有一个简单的UITextView,里面有一个电子邮件链接。文本视图是可选择的并检测链接。这样,您可以单击电子邮件,它会以模态方式打开 MFMailComposeViewController 视图控制器。

但是,我在应用启动时做了一些自定义:

[[UINavigationBar appearance] setBarTintColor: myGreyColor];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: myFont}];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

这样,所有的导航栏都是灰色的,有白色的文字和白色的按钮,标题有自定义字体。

我的问题是所有这些都不适用于邮件编写器:栏是灰色的,标题是白色的,但字体是默认的 helvetica neue,按钮是默认的蓝色。而且,状态栏是黑色的,即使我的 Info.plist 显示 UIStatusBarStyleLightContentView controller-based status bar appearance 设置为 NO

当我手动调用MFMailComposeViewController 时,我知道如何自定义它,但在这里它会自动弹出。如何应用我的样式?

【问题讨论】:

  • @rdurand我遇到了与 MFMailComposeViewController 导航颜色相同的问题。你能建议我做同样的事情吗?
  • @APG :我写了一个答案。最好的方法是手动构建您的 MFMailComposeViewController 并直接对其进行自定义。然后截取UITextView中的链接点击,自己呈现邮件viewcontroller。
  • @APG :我更新了我的答案。经过一些研究,看起来自定义邮件编写器是一个非常糟糕的主意,并且会让您的应用程序被 Apple 拒绝。当您更改外观时,您应该使用 appearanceWhenContainedIn 来定位您自己的视图控制器,并让邮件编写器独自工作。

标签: ios uitextview mfmailcomposeviewcontroller


【解决方案1】:

编辑

自定义MFMailComposeViewController 的外观是一个非常糟糕的主意,很可能会导致您的应用被Apple 拒绝。仅当您不打算将应用提交给 Apple 时,才应使用以下解决方案。


看起来我解决了,感谢Ray Wenderlich(再次......)。这是完整的代码:

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

【讨论】:

  • 当电子邮件被输入到 NSString 并设置为 UITextview 的文本(即不使用属性字符串)时,[URL 方案] 应该是“mailto”
【解决方案2】:

那么你应该通过以下外观方法而不是修改整个应用程序导航栏:-

[[UINavigationBar appearanceWhenContainedIn:<#(__unsafe_unretained Class<UIAppearanceContainer> *), ...#>, nil]setTintColor:[UIColor greenColor]];

因为您的 MFMailComposeViewController 打开到您的应用程序并且您正在修改整个应用程序的导航栏,所以 MFMailComposeViewController 的导航栏正在修改的原因。 使用上面的外观方法可以修改选中的类或者父类,通过它可以修改派生类,这不会修改MFMailComposeViewController,因为它不会是你父类的一部分。

【讨论】:

    【解决方案3】:

    斯威夫特 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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      • 2014-12-22
      • 1970-01-01
      • 2014-06-10
      相关资源
      最近更新 更多