当从iOS7中的UIActivityViewController显示邮件生成器中的“发送”和“取消”按钮时,无法设置其文本颜色
我正在使用UIActivityViewController共享iOS7中的项目。当我点击Mail选项时,它会弹出Mail composer,但导航栏上的Cancel和Send按钮以及导航栏本身都是蓝色的,这使得阅读非常困难,因此我想更改它们的颜色。它在iOS6中工作,但在iOS7中不工作 我试过了当从iOS7中的UIActivityViewController显示邮件生成器中的“发送”和“取消”按钮时,无法设置其文本颜色,ios7,uiactivityviewcontroller,Ios7,Uiactivityviewcontroller,我正在使用UIActivityViewController共享iOS7中的项目。当我点击Mail选项时,它会弹出Mail composer,但导航栏上的Cancel和Send按钮以及导航栏本身都是蓝色的,这使得阅读非常困难,因此我想更改它们的颜色。它在iOS6中工作,但在iOS7中不工作 我试过了 [[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIC
[[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]];
这会导致应用程序第一次运行时,颜色会闪烁红色,然后立即切换回蓝色。我们有理由不能改变苹果代码中的用户界面。主要是因为它是苹果的。它们不允许您以任何方式编辑MFMailComposer ViewController中UI的外观。如果有办法的话,我就不知道了,但我从来没有见过任何办法。MFMailComposeViewController不支持外观属性,因为它是在iOS 3.0中创建的,并且外观直到iOS 5.0才成为一种东西 以下是指向MFMailComposeViewController apple文档的链接:
希望这有帮助 如果要在iOS 7中设置“取消”和“发送”按钮的颜色,应使用以下选项:
// Change the colours of the buttons in iOS 7
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
在iOS 6中,确实是这样的,您也应该在代码中保留这一点:
// Change the colours of the buttons in iOS 6
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
// Change the color of the the navigation bar in iOS 6 and 7
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
请尝试此代码,因为它可能会对您有所帮助
[[mailComposer navigationBar] setTintColor:[UIColor blackColor]];
对于ios7,我认为您应该检查一下这段代码
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
如果它也不工作,请尝试internet上提供的Mail Compose View Controller apple文档。在演示Mail composer之前,插入以下行:
[mailComposer.navigationBar setTintColor:[UIColor whiteColor]];
[self presentViewController:mailComposer animated:YES completion:nil];
[self presentViewController:mailComposer animated:YES completion:^{[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];}];
NSArray *activities=@[self]; //And set self to be a UIActivityItemSource
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:activities applicationActivities:nil];
即使我在应用程序完成启动时设置了状态栏样式,我也需要在完成块中再次设置它,如下所示:
[mailComposer.navigationBar setTintColor:[UIColor whiteColor]];
[self presentViewController:mailComposer animated:YES completion:nil];
[self presentViewController:mailComposer animated:YES completion:^{[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];}];
NSArray *activities=@[self]; //And set self to be a UIActivityItemSource
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:activities applicationActivities:nil];
这似乎是iOS 7的一个bug。我在网上看到了其他关于这方面的报道。在iOS 7.1中,它似乎也没有被修复
具体而言,无论您做什么,都无法在UIActivityViewController显示的对话框的导航栏上设置着色颜色。成功更改
MFMailComposer视图控制器中UINavigationBar
上的发送和取消按钮的文本颜色(从UIActivityViewController
显示时,发送和取消)和MFMessageComposeViewController
(仅取消)
使用UIActivityViewController,点击消息
或邮件
:
您会注意到发送和取消按钮的默认文本颜色为蓝色:
要更改此选项,请在AppDelegate.m
类的didfishlaunchwithoptions
方法中插入以下行:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor whiteColor]];
这导致:
您还可以使用其他颜色,例如:
[UIColor purpleColor];
[UIColor greenColor];
我是如何测试的?我注意到这个解决方案适用于以下方面:
self.navigationController?.presentViewController(activityViewController, animated: true, completion: { () in
UIBarButtonItem.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().barTintColor = UIColor.whiteColor() // optional to change bar backgroundColor
}
- 使用Xcode 5.1,在iOS 7.1模拟器中,构建为基础iOS SDK 7.1(可以从选择项目文件->构建设置->基础SDK中选择。也可以从常规->部署目标->7.1中选择)
- 使用Xcode 5.1,在iPhone 4上构建为基本iOS SDK 7.0(可以从选择项目文件->构建设置->基本SDK中选择。也可以从常规->部署目标->7.0中选择)
- 使用Xcode 5.1,在iPhone 4上构建为基本iOS SDK 7.1(可以从选择项目文件->构建设置->基本SDK中选择。也可以从常规->部署目标->7.1中选择)
使用测试时,它不起作用:
self.navigationController?.presentViewController(activityViewController, animated: true, completion: { () in
UIBarButtonItem.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().barTintColor = UIColor.whiteColor() // optional to change bar backgroundColor
}
- 使用Xcode 5.1,在iOS 7.0模拟器中,构建为基本iOS SDK 7.0(可以从选择项目文件->构建设置->基本SDK中选择。也可以从常规->部署目标->7.0中选择)
因此,使用它应该是安全的,因为我相信实际设备上的行为比iOS模拟器中的行为更重要。
如果有人知道为什么它不能在iOS 7.0模拟器中工作,我想知道。:我的应用程序也有同样的问题,UINavigationBar
的tintColor
属性由于外观代理到处都是白色。由此产生的效果是,mail composer视图控制器导航栏中的UIBarButtonItem
不可见(白色导航栏上的白色按钮)
我在我的应用程序:didFinishLaunchingWithOptions:
方法中有此调用:
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
由于(目前?)无法访问UIActivityViewController
中mail composer视图控制器的UINavigationBar
,因此我根据Alex的回答进行了以下变通:
UIColor *normalColor = [[UINavigationBar appearance] tintColor];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
[activityViewController setCompletionHandler:^(NSString *activityType, BOOL completed) {
// back to normal color
[[UINavigationBar appearance] setTintColor:normalColor];
}];
[self presentViewController:activityViewController animated:YES completion:^{
// change color temporary
[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:232.0f/255.0f green:51.0f/255.0f blue:72.0f/255.0f alpha:1.0f]];
}];
PS:此代码适用于iOS 7,但您可以在iOS 6中使用[[uibarbuttonite外观]setTintColor:][/code>(参见Kevin van Mierlo的回答)我无法让Alex的解决方案起作用,然而,尽管我必须在我的情况下设置barTintColor和titleTextAttributes,我还是成功地获得了Paillou工作答案的变体:
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
activityViewController.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo, UIActivityTypePostToFlickr, UIActivityTypeAirDrop];
[activityViewController setCompletionHandler:^(NSString *activityType, BOOL completed) {
// back to normal color
[[UINavigationBar appearance] setBarTintColor:AAColorInputBorder];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Avenir-Medium" size:18], NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName,
nil]];
}];
[self presentViewController:activityViewController animated:YES completion:^{
// change color temporary
[[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Avenir-Medium" size:18], NSFontAttributeName,
AAColorInputBorder, NSForegroundColorAttributeName,
nil]];
谢谢你,派洛 这对我很有用:
在函数的AppDelegate.m中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
我已输入以下代码:
//mail composer
[[UINavigationBar appearanceWhenContainedIn:[MFMailComposeViewController class], nil] setBarTintColor:myBackgroundColor];
[[UINavigationBar appearanceWhenContainedIn:[MFMailComposeViewController class], nil] setTintColor:myBarItemsColor];
它在iOS7+iOS8上运行良好,没有在较新版本上尝试以下是截至今天在iOS7.1上运行的内容
子类化UIActivityViewController并重写以下方法:
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion
{
viewControllerToPresent.view.tintColor = [UIColor whiteColor];
[super presentViewController:viewControllerToPresent animated:flag completion:^{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
if (completion) {
completion();
}
}];
}
这将使按钮变白,状态栏变白。对于Swift:
self.navigationController?.presentViewController(activityViewController, animated: true, completion: { () in
UIBarButtonItem.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().barTintColor = UIColor.whiteColor() // optional to change bar backgroundColor
}
这会将发送和取消按钮颜色更改为白色(在iOS 7,8上测试),但我仍然无法将状态栏文本颜色设置为白色。(尽管我没有尝试过该子类UIActivityViewController
so
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
}
}
extension UIActivityViewController {
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UINavigationBar.appearance().barTintColor = .white
}
open override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.isOpaque = false
navigationController?.navigationBar.barTintColor = UIColor(red: (247/255), green: (247/255), blue: (247/255), alpha: 1)
//navigationBar.tintColor = UIColor.white
}
open override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
UINavigationBar.appearance().barTintColor = mycustomColor
}
}
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)
- (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
}
[[UINavigationBar appearance] setBarTintColor:[UIColor kNavigationBarBackgroundColor]]; //Again, this is my app's custom nav bar background color
NSArray *activities=@[self]; //And set self to be a UIActivityItemSource
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:activities applicationActivities:nil];