【问题标题】:Buttons on navigation bar do not pick up window tint colour导航栏上的按钮不拾取窗口色调颜色
【发布时间】:2014-08-31 16:37:58
【问题描述】:

注意,我最近又回到了这个问题,它似乎已在更新版本的 SDK 中得到修复,除了问题中的代码之外,我无需实现任何东西。非常感谢所有回答的人。

我有一个在 iPad 上运行的 Objective-C 应用程序并显示一个视图控制器,其模态表示样式为 UIModalPresentationPageSheet

UINavigationController *editorNavigationController = [[UINavigationController alloc] initWithRootViewController:editorViewController];
editorNavigationController.modalPresentationStyle = UIModalPresentationPageSheet;
[navigationController presentViewController:editorNavigationController animated:YES completion:nil];

当显示这个视图控制器时,导航栏中的按钮是紫色的,我认为这是从窗口的色调中提取出来的,这就是我想要的。

稍后我需要在顶部显示另一个视图控制器,它会填满整个窗口:

UINavigationController *previewNavigationController = [[UINavigationController alloc]initWithRootViewController:myPreviewViewController];
[owningViewController presentViewController:previewNavigationController animated:YES completion:nil];

我的问题是当显示myPreviewController 时,导航栏中的按钮是灰色的。我尝试恢复新导航控制器的颜色:

previewNavigationController.navigationBar.tintColor = [UIColor colorWithRed:123/255.0 green:26/255.0 blue:69/255.0 alpha:1];

但没有任何快乐。

如何使按钮具有正确的颜色?我可以让新的导航控制器自动选择窗口色调,还是必须明确设置?这与在使用UIModalPresentationPageSheet 的导航控制器的顶部显示第二个导航控制器有关吗?

非常感谢任何帮助!谢谢,

保罗

【问题讨论】:

  • 有截图吗?也许……
  • 尝试在myPreviewViewControllerviewDidLoad中设置self.navigationController.navigationBar.tintColor
  • 感谢@kambala,但不幸的是没有解决它。
  • 你试过用ios的外观方法吗?喜欢:[[UINavigationBar 外观] setBarTintColor:[UIColor redColor]]; [[UINavigationBar 外观] setTintColor:[UIColor grayColor]];
  • 感谢@Alexander,但这也没有解决问题。我尝试在应用程序委托中调用 setTintColor,并在展示新导航控制器之前立即调用,但仍然没有任何乐趣。

标签: ios objective-c ipad uinavigationcontroller


【解决方案1】:

您可以将navigationBar设置为半透明和透明。 在视图Will Appear

self.navigationController.navigationBar.translucent = NO;

比创建一个框架大小为 navigationBar (CGRectMake(0,0,self.view.frame.size.height,22) 的 UIView 并在其上创建具有所需颜色的按钮。 我知道,这是一个拐杖,但应该可以)

【讨论】:

    【解决方案2】:

    您可以全局更改UIBarButtonItem 的外观,这样所有UINavigationControllers 将共享相同的设计:

    [[UIBarButtonItem appearance] setTintColor:[UIColor purpleColor]];
    [[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                              [UIFont fontWithName:@"fontName" size:16.0f],NSFontAttributeName,
                                                              nil] forState:UIControlStateNormal]; 
    

    此外,您还可以更改[UINavigationBar appearance]

    //The setTintColor would tint the < Back button in the NavigationBar 
    [[UINavigationBar appearance] setTintColor:[UIColor greenColor]];
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
    [[UINavigationBar appearance] setTitleTextAttributes:
         @{NSForegroundColorAttributeName:[UIColor blueColor]}];
    

    此代码可以在呈现UIViewControllers 之前添加,也可以简单地添加在AppDelegate.m 中。

    【讨论】:

      猜你喜欢
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多