【问题标题】:Modal status bar and navigation bar text colors from UIActivityViewControllers in iOS 7iOS 7 中 UIActivityViewControllers 的模态状态栏和导航栏文本颜色
【发布时间】:2013-11-05 16:47:36
【问题描述】:

当我使用UIActivityViewController 时,在用户选择一个活动(例如邮件或消息)后,我无法更改状态栏的文本颜色,也无法更改取消和发送导航的文本/色调颜色栏按钮。对于条形按钮,在我尝试使用的AppDelegate 中:

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

然后什么也没有发生。但是我可以用这个设置导航栏标题:

    [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor, nil]];

我在Info.plist 中将UIViewControllerBasedStatusBarAppearance 设置为NO。并把这条线:

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

AppDelegate 中,并且根本没有改变状态栏颜色的运气。有什么想法吗?

【问题讨论】:

  • 已经试过这个状态栏颜色stackoverflow.com/questions/17678881/…
  • @the1pawan 我试过了。它只会更改我的常规 UIViewControllers 的状态栏颜色,并且不会影响从 UIActivityViewController 启动的活动的状态栏
  • 我向 Apple 寻求帮助,他们确认这是一个错误。我已经提交了一个错误报告,所以希望这将在 iOS 7.1 中得到修复。我建议同时在 bugreport.apple.com 上提交错误报告并参考我的 15959753。

标签: ios7 uinavigationbar uiactivityviewcontroller textcolor tintcolor


【解决方案1】:

由于 UIActivityViewController 呈现底层模型视图控制器,我们使用此解决方法来修复状态栏颜色问题:

@interface StatusBarColorApplyingActivityViewController : UIActivityViewController

@end

@implementation StatusBarColorApplyingActivityViewController

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
  [super presentViewController:viewControllerToPresent animated:flag completion:^{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    if (completion) {
      completion();
    }
  }];
}

@end

如您所见,这只是一个扩展 UIActivityViewController 的类,覆盖了presentViewController:animated:completion:。当视图控制器出现时,我们通过 UIApplication 在完成块中设置状态栏样式。然后我们调用给该方法的原始完成块,如果有的话。

【讨论】:

  • 在 iOS 8 中覆盖 presentViewController:animated:completion 不起作用,因此我覆盖了 viewWillAppearviewWillDissappear 以在样式之间来回切换。
  • 以及状态栏颜色是如何指代问题的?
【解决方案2】:

不是从UIActivityViewController 子类化,我们可以在呈现导航栏时更改它的tintColor,并在completionHandler 完成后恢复它。例如:

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 to suit your need
     [[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:25.0f/255.0f green:125.0f/255.0f blue:255.0f/255.0f alpha:1.0f]]; // ActionSheet options' Blue color
}];

【讨论】:

    【解决方案3】:

    在 iOS 8 中,UIActivityViewController 在应用程序的根视图控制器上显示其单独的撰写控制器。

    您需要子类化您的根视图控制器(无论是 UIViewController 还是 UINavigationController)并添加以下代码。

    @interface UINavigationControllerBarColor : UINavigationController
    
    @end
    
    @implementation UINavigationControllerBarColor
    
    - (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
        [super presentViewController:viewControllerToPresent animated:flag completion:^{
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
            if (completion) {
                completion();
            }
        }];
    }
    
    @end
    

    然后,不要在 AppDelegate 或故事板中初始化 UINavigationController,而是初始化新的子类控制器。

    其他一些建议是 UIActivityViewController 的子类,但这不起作用。

    如果您还想更改栏按钮和标题颜色,请在您的应用程序中使用以下代码:didFinishLaunching:

    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                          [UIColor whiteColor], UITextAttributeTextColor,
                                                          [UIFont systemFontOfSize:18.0f], UITextAttributeFont,
                                                          nil]];
    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor whiteColor]];
    

    【讨论】:

    • 我按照您在第一部分中的建议做了,但它从未被调用。我已经将我的根视图控制器子类化为处理 shouldAutorotate 效果很好,但 presentViewController 没有被调用。还有其他细节吗?
    • 它是最顶层的视图控制器的根。
    【解决方案4】:

    我相信更改导航栏颜色的代码是这样的:

    [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
    

    这是用于更改 iOS 7 中导航栏按钮的颜色,如果您可能需要的话:

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

    如果您想在 iOS 6 中更改按钮的颜色,这就是代码:

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

    此代码也适用于 iOS 8+,用于更改 UIActivityViewController 活动的栏按钮文本颜色(例如通过消息或邮件编辑器共享)。

    【讨论】:

    • 我正在为 iOS 8+ 开发一个应用程序,仅供参考,我的应用程序中需要最后一个(UIBarButtonItem 外观 setTintColor)来更改 UIActivityViewController 活动的栏按钮文本颜色(例如通过消息共享或邮件作曲家)。
    • @MasonG.Zhwiti 感谢您的回复。我将其添加到我的答案中。
    【解决方案5】:

    我找到了更改发送取消按钮的文本颜色的解决方案。

    查看我来自here的回答。

    关于将 状态栏样式 从黑色更改为白色,我几乎尝试了 Stackoverflow 上的所有内容,但没有任何效果。似乎没有解决方法。

    一件事可能有效,但我真的不知道如何使用它,可能是更改子视图控制器中的状态栏样式。 Stackoverflow 上有一篇关于它的帖子 here

    这可能仅在假设MFMailComposerViewControllerMFMessageComposeViewControllerUIActivityViewController 的子视图控制器时才有效,因此如果我们为UIActivityViewController 指定状态栏样式,那么子视图控制器应该具有相同的状态栏样式作为父级。

    UIViewController 中有一个名为childViewControllerForStatusBarStyle 的方法。 Here 是它的 Apple 文档。

    但我真的不知道如何使用它。有人发现了吗?

    【讨论】:

      【解决方案6】:

      你必须设置整个应用的tintColor。

      self.window.tintColor = [UIColor redColor];
      

      self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
      

      条形按钮

      self.navigationController.navigationBar.tintColor = [UIColor redColor];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-26
        • 2019-04-20
        • 1970-01-01
        • 2013-10-04
        • 1970-01-01
        • 2014-06-23
        • 1970-01-01
        相关资源
        最近更新 更多