在iOS7中,当从UIActivityViewController中呈现邮件组合器时,无法设置发送和取消按钮的文本颜色。

68
我正在使用UIActivityViewController在iOS7上分享项目。当我点击邮件选项时,会弹出邮件撰写器,但是导航栏上的取消和发送按钮以及导航栏本身都是蓝色的,使得它们很难阅读,所以我想要改变它们的颜色。在iOS6中可以工作,但在iOS7中不行。
我已经尝试过。
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], UITextAttributeTextColor, [UIColor clearColor], UITextAttributeTextShadowColor, nil] forState:UIControlStateNormal];

它在iOS6中可用,我已经尝试过。

[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];

在应用程序第一次运行时,它会导致颜色闪烁为红色,然后立即切换回蓝色。


1
我遇到了同样的问题。希望有人知道是怎么回事。 - Kevin_TA
这里也有同样的问题...你找到解决方案/解决方法了吗? - rgomesbr
1
我不知道你是否还在关注这个问题,但是我给出了一个解决方案。iOS 7按钮颜色是使用导航栏的色调颜色设置的。 - Kevin van Mierlo
20个回答

1
尝试使用这段代码,也许它会对你有所帮助。
[[mailComposer navigationBar] setTintColor:[UIColor blackColor]];

1
很明显我们无法改变苹果代码中UI的样式,这主要是因为它是苹果的。他们不允许您以任何方式编辑MFMailComposerViewController中的UI外观。如果有方法,那么我不知道,但我从未见过任何方法可以这样做。MFMailComposeViewController不支持外观属性,因为它是在iOS 3.0中创建的,而外观则是在iOS 5.0中才成为一种功能。

以下是MFMailComposeViewController苹果文档链接: MFMailComposeViewController

希望这能帮到你!


0

我在这方面遇到了很大的麻烦,特别是当MFMailComposeViewController/MFMessageViewController本身由UIActivityViewController显示时。

我不得不使用方法交换来撤消和重新执行我的应用程序对颜色和字体的自定义,一些帮助来自https://github.com/rentzsch/jrswizzle

SwizzledComposeViewControllers.h

#import <MessageUI/MessageUI.h>

@interface MFMailComposeViewController (GMSwizzling)
@end

@interface MFMessageComposeViewController (GMSwizzling)
@end

SwizzledComposeViewControllers.m

#import "SwizzledComposeViewControllers.h"
#import "AppDelegate.h"
#import "JRSwizzle.h"

@implementation MFMailComposeViewController (GMSwizzling)

+ (void)load {
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    [self jr_swizzleMethod:@selector(init) withMethod:@selector(gmswizzled_init) error:nil];
    [self jr_swizzleMethod:@selector(viewWillAppear:) withMethod:@selector(gmswizzled_viewWillAppear:) error:nil];
    [self jr_swizzleMethod:@selector(viewWillDisappear:) withMethod:@selector(gmswizzled_viewWillDisappear:) error:nil];
  });
}

- (instancetype)gmswizzled_init {
  [(AppDelegate*)UIApplication.sharedApplication.delegate uncustomiseAppearance];
  return [self gmswizzled_init];
}

- (void)gmswizzled_viewWillAppear:(BOOL)animated {
  [(AppDelegate*)UIApplication.sharedApplication.delegate uncustomiseAppearance];
  [self gmswizzled_viewWillAppear:animated];
}

- (void)gmswizzled_viewWillDisappear:(BOOL)animated {
  [(AppDelegate*)UIApplication.sharedApplication.delegate customiseAppearance];
  [self gmswizzled_viewWillDisappear:animated];
}

@end


@implementation MFMessageComposeViewController (GMSwizzling)

+ (void)load {
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    [self jr_swizzleMethod:@selector(init) withMethod:@selector(gmswizzled_init) error:nil];
    [self jr_swizzleMethod:@selector(viewWillAppear:) withMethod:@selector(gmswizzled_viewWillAppear:) error:nil];
    [self jr_swizzleMethod:@selector(viewWillDisappear:) withMethod:@selector(gmswizzled_viewWillDisappear:) error:nil];
  });
}

- (instancetype)gmswizzled_init {
  [(AppDelegate*)UIApplication.sharedApplication.delegate uncustomiseAppearance];
  return [self gmswizzled_init];
}

- (void)gmswizzled_viewWillAppear:(BOOL)animated {
  [(AppDelegate*)UIApplication.sharedApplication.delegate uncustomiseAppearance];
  [self gmswizzled_viewWillAppear:animated];
}

- (void)gmswizzled_viewWillDisappear:(BOOL)animated {
  [(AppDelegate*)UIApplication.sharedApplication.delegate customiseAppearance];
  [self gmswizzled_viewWillDisappear:animated];
}

@end

(我必须承认我记不得为什么在initviewWillAppear中都没有定制外观,但我相当确定有一个原因...)。

0
在Swift中,我为UIViewController编写了一个扩展:
extension UIViewController {

    func presentActivityViewController(viewControllerToPresent: UIViewController) {
        self.presentViewController(viewControllerToPresent, animated: true) { _ in
            UIBarButtonItem.appearance().tintColor = UIColor.whiteColor()
            UINavigationBar.appearance().barTintColor = Config.primaryColor
        }
    }
}

当我需要呈现UIActivityViewController时,我调用:

    let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: [])
    presentActivityViewController(activityViewController)

0

在展示UIActivityViewController之前,您可以设置您的appearance。将外观重置添加到您的activity VCcompletionWithItemsHandler中:

setNavBarAppearance()

activityVC.completionWithItemsHandler = { [weak self] _, _, _, _ in
    self?.resetNavBarAppearance()
}

present(activityVC, animated: true, completion: nil)

唯一的问题是,如果活动类似于邮件发送,则为全屏。您的外观不会应用于当前可见的视图。一些小技巧可以解决这个问题:
setNavBarAppearance()

activityVC.completionWithItemsHandler = { [weak self] _, _, _, _ in
    self?.resetNavBarAppearance()

    // Hacks(choose one of them):
    // 1)
    self?.navigationController?.isNavigationBarHidden = true
    self?.navigationController?.isNavigationBarHidden = false
    // 2)
    let redrawTriggerVC = UIViewController()
    redrawTriggerVC.modalPresentationStyle = .popover
    self.present(redrawTriggerVC, animated: false, completion: nil)
    redrawTriggerVC.dismiss(animated: false, completion: nil)
}

present(activityVC, animated: true, completion: nil)

0
在iOS9上使用Swift编程时,设置
UINavigationBar.appearance().barTintColor = UIColor.greenColor() // eg
UINavigationBar.appearance().translucent = false

在呈现活动视图控制器之前,这对我很有用。


0

我还没有找到一个我喜欢的机制,所以就算只是为了参考,这里是我的方法。问题的一部分在于iOS的后续版本增加了应用程序添加系统范围共享和操作扩展的功能。这些第三方项目似乎以各种方式编码。有些继承了应用程序的导航栏样式,有些使用自己的样式,有些似乎假定白色导航栏(但实际上是从应用程序继承的)。

这在iOS 12.2上进行了测试。

我创建了一个UIActivityItemSource,其中我有:

- (nullable id)activityViewController:(nonnull UIActivityViewController *)activityViewController itemForActivityType:(nullable UIActivityType)activityType {
    if (activityType == UIActivityTypePrint || [activityType.lowercaseString containsString:@"extension"] || [activityType containsString:@"AssignToContact"]) {
        //What a hack, but the best I can do.  Seems some extensions inherit nav style from parent, others don't.
        //ActionExtension is bottom row; all those I tested need this.  The string comparison catches most non-OS extensions (the type is set by developer).
        [[UINavigationBar appearance] setBarTintColor:[UIColor kNavigationBarBackgroundColor]]; //kNavigationBarBackgroundColor is my app's custom nav bar background color
    } else {
        [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
    }
    return self.pdfData; //In my case I'm sharing a PDF as NSData - modify as needed for your shared item
}

然后在我的UIActivityViewControllercompletionWithItemsHandler中包含:

[[UINavigationBar appearance] setBarTintColor:[UIColor kNavigationBarBackgroundColor]]; //Again, this is my app's custom nav bar background color

与特定问题无关,但如果您目前没有UIActivityItemSource,则需要像这样做:

NSArray *activities=@[self]; //And set self to be a UIActivityItemSource
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:activities applicationActivities:nil];

我相信这并不是100%可靠的,但它适用于我尝试过的所有扩展。


0

对于iOS7,我认为你应该浏览这段代码

[[UINavigationBar appearance] setTintColor:[UIColor redColor]];

如果还是不起作用,那么可以尝试在互联网上查找Apple文档中提供的邮件撰写视图控制器。


0
在显示邮件撰写器之前,像这样插入这行代码:
[mailComposer.navigationBar setTintColor:[UIColor whiteColor]];
[self presentViewController:mailComposer animated:YES completion:nil];

尽管我在应用程序完成启动时设置了状态栏样式,但我还需要在完成块中再次设置它,就像这样:

[self presentViewController:mailComposer animated:YES completion:^{[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];}];

7
问题是关于 UIActivityViewController 而不是 MFMailComposeViewController。 - Sjoerd Perfors

0

我在iOS 9和10中尝试了许多不同的方法,但这是唯一有效的方法。请注意,我的navigationBar后面还有一个背景图像:

[UIApplication.sharedApplication setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
NSDictionary *attribs = @{NSForegroundColorAttributeName:UIColor.whiteColor};
UINavigationBar.appearance.titleTextAttributes = attribs;
UINavigationBar.appearance.tintColor = UIColor.whiteColor;
[UINavigationBar.appearance setBackgroundImage:[UIImage imageNamed:@"IOSNavigationBar"] forBarMetrics:UIBarMetricsDefault];
UIBarButtonItem.appearance.tintColor = UIColor.whiteColor;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接