首页 文章

iOS 7中UIActivityViewControllers的模态状态栏和导航栏文本颜色

提问于
浏览
17

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

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

没有任何反应 . 但是我可以用这个设置导航栏 Headers :

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

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

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

AppDelegate 中,根本没有改变状态栏颜色的运气 . 有任何想法吗?

6 回答

  • 0

    由于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设置状态栏样式 . 然后我们调用给出方法的原始完成块(如果有的话) .

  • 2

    我们可以在呈现它时更改导航栏的tintColor,并在 completionHandler 完成后将其还原,而不是从 UIActivityViewController 进行子类化 . 例如:

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

    在iOS 8中,UIActivityViewController在应用程序的 root view controller 上显示其各自的组合控制器 .

    您需要子类化根视图控制器(无论是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或storyboard中初始化UINavigationController,而是初始化新的子类控制器 .

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

    如果要更改条形按钮和 Headers 颜色,请在应用程序中使用以下内容: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]];
    
  • -1

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

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

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

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

    如果你想改变iOS 6中按钮的颜色,这就是代码:

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

    此代码也适用于iOS 8,以更改UIActivityViewController活动的条形按钮文本颜色(如通过Messages或Mail Composer共享) .

  • 4

    我找到了一个改变 SendCancel 按钮的文本颜色的解决方案 .

    检查here的答案 .

    关于将_1744150从黑色更改为白色,我已经尝试了Stackoverflow上的所有内容,但没有任何效果 . 似乎没有解决方法 .

    有一件事可能会奏效,但我不会发表关于它的Stackoverflow帖子here .

    这可能只有在 MFMailComposerViewControllerMFMessageComposeViewControllerUIActivityViewController 的子视图控制器的假设下才有效,因此如果我们为 UIActivityViewController 指定状态栏样式,则子视图控制器应具有与父视图相同的状态栏样式 .

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

    但我真的不知道如何使用它 . 有没有人想到这一点?

  • 2

    您必须设置整个应用程序的tintColor .

    self.window.tintColor = [UIColor redColor];
    

    要么

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

    酒吧按钮

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

相关问题