【问题标题】:UINavigationBar set tintcolor tested in iOS7 not working?在 iOS7 中测试的 UINavigationBar 设置 tintcolor 不起作用?
【发布时间】:2013-06-07 03:57:56
【问题描述】:

我有一个具有UINavigationBar 的应用程序,并且我已将色调设置为黑色,如下所示:

self.navigationController.navigationBar.tintColor = [UIColor blackColor];`

我在 IOS 6 中测试过,它是黑色的。但是,当我在 iOS 7 中尝试相同的应用程序时,它显示为默认导航栏。

正如标题所说,它不起作用吗?

【问题讨论】:

  • 这里有同样的问题。其他一切正常
  • //for ios6 [Helper navController].navigationBar.tintColor = [Helper getThemeColorWithTransparent:NO]; //对于ios7,不工作 if ([[Helper navController].navigationBar respondsToSelector:@selector(setBarTintColor:)]) { [[Helper navController].navigationBar performSelector:@selector(setBarTintColor:) withObject:[Helper getThemeColorWithTransparent:NO] ];我尝试了上述方法,但仍然无法正常工作。这很奇怪。如果基本 sdk 是 6,即使在 iOS7 上也应该没问题
  • beta 2 更新修复了这个问题。
  • if ([self.navigationController.navigationBar respondsToSelector:@selector(setBarTintColor:)]) { self.navigationController.navigationBar.barTintColor = [self backgoundColorForCurrentCompany]; }
  • @veereef:试试这个......self.navigationController.navigationBar.barTintColor = [UIColor redColor];

标签: iphone objective-c ios7 uinavigationcontroller tintcolor


【解决方案1】:

您需要设置 barTintColor 属性。

您可以使用 Tint (barTintColor) 字段为导航栏背景指定自定义色调颜色。默认背景色为白色。

来自 iOS7 文档:https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/UINavigationBar.html#//apple_ref/doc/uid/TP40012857-UINavigationBar-SW1

【讨论】:

  • 但问题是,如果你的基础 sdk 是 6.0,颜色应该是正确的。奇怪的
  • 这在 iOS7 中对我不起作用,但问题中的原始代码确实有效......
【解决方案2】:

以下代码对我有用:

self.navigationController.navigationBar.barTintColor = [UIColor blackColor];

【讨论】:

    【解决方案3】:

    我使用以下代码更改 iOS7 中导航栏的色调,我在应用程序委托“applicationDidFinishLaunch”方法中添加了这个,它对我来说很好:

    /* ios 7 Change */
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
        {
            [[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x4B678B)];
            NSShadow *shadow = [[NSShadow alloc] init];
            shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
            shadow.shadowOffset = CGSizeMake(0, 1);
            [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                                   [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
                                                                   shadow, NSShadowAttributeName,
                                                                   [UIFont fontWithName:@"Helvetica Neue" size:21.0], NSFontAttributeName, nil]];
            // self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
            //[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
            [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    
        }
    

    【讨论】:

    • 这条线对我来说很神奇。我在 iOS7.1 中需要的只是导航颜色似乎坏了。 [[UINavigationBar 外观] setBarTintColor:UIColorFromRGB(0x4B678B)];
    【解决方案4】:
        [UINavigationBar appearance].tintColor = [UIColor redColor];
        if ([[UINavigationBar appearance] respondsToSelector:@selector(setBarTintColor:)]) {
            [UINavigationBar appearance].barTintColor   = [UIColor redColor];
        }
    

    //或

        self.navigationController.navigationBar.tintColor = [UIColor redColor];
        if ([self.navigationController.navigationBar respondsToSelector:@selector(setBarTintColor:)]) {
            self.navigationController.navigationBar.barTintColor    = [UIColor redColor];
        }
    

    【讨论】:

      【解决方案5】:

      有同样的问题,但通过故事板文件解决了。

      试试下面的。

      1. 打开您的 .storyboard 文件。
      2. 使用导航控制器选择场景
      3. 选择导航栏项目进入导航控制器场景

      4. 打开 XCode 右侧的 Utilities 选项卡

      5. 打开属性检查器
      6. 在“导航栏”组中,将有 Bar Tint 下拉列表。您可以为色调选择任何颜色。

      【讨论】:

        【解决方案6】:

        Fernando 和 sanjana 的答案是关键,但我将添加一些内容以使其更清晰、更明显。

        导航栏有两个属性

        • tintColor
        • barTintColor

        如果您不考虑 iOS 7 术语,这有点误导。

        tintColor 更改导航栏上按钮的颜色。 要更改背景颜色,需要设置属性barTintColor

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

        这段代码 sn-p 会给你一个带有绿色按钮的白色导航栏。

        【讨论】:

        • tintColor 不仅可以应用于导航栏上的按钮。包含透明像素图像的 UIImageView 可以通过其父视图的 tint 属性“着色”。因此,如果我有一个 tintColor = [UIColor blueColor] 的表格视图单元格,我的图像在添加到单元格的内容视图时会显示为蓝色。
        猜你喜欢
        • 2014-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多