更改UINavigationBar颜色

时间:2014-02-07 07:27:13

标签: ios iphone objective-c ios7 uinavigationbar

我正在开发iOS 7应用。我的导航栏过去看起来如下图所示:

enter image description here

但是在添加这段代码之后

self.edgesForExtendedLayout = UIRectEdgeNone;

导航车颜色变暗,如下图所示:。enter image description here

我们如何让导航栏像第一张图片一样保持亮度,同时保持上面的代码?

4 个答案:

答案 0 :(得分:3)

默认情况下,导航栏的半透明属性设置为YES。
 此外,系统模糊应用于所有导航栏。在此设置下,iOS 7会降低条形图的颜色。

差异半透明设置

Difference Translucent settings 设置色调 Setting Tint Color

关闭半透明设置

Turn off translucent setting

将此代码放入didFinishLaunchingWithOptions的appDelegate.m中:

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

if (floor(NSFoundationVersionNumber) >= NSFoundationVersionNumber_iOS_6_1)
{
    // Load resources for iOS 7 or later


// To change the background color of navigation bar
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x067AB5)];

// To change the color of back button
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];


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:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];

}

答案 1 :(得分:0)

试试这个

对于iOS7,导航栏的颜色可以通过这几行改变。

if(IS_IOS7){

    //Your color code
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:34.0/255.0 green:59.0/255.0 blue:135.0/255.0 alpha:1.0];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.titleTextAttributes
    = @{UITextAttributeTextColor : [UIColor whiteColor]};
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
}

答案 2 :(得分:0)

在iOS 7中,导航栏的tintColor会影响后指示图像,按钮标题和按钮图像的颜色。 barTintColor属性会影响条形本身的颜色。此外,导航栏默认为半透明。关闭或打开半透明不会影响按钮,因为它们没有背景。

在appdelegate中添加此代码

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        [[UINavigationBar appearance] setBarTintColor:[UIColor yourColorCode]];

//optional 

        NSShadow *shadowObj = [[NSShadow alloc] init];
        shadowObj.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
        shadowObj.shadowOffset = CGSizeMake(0, 1);
        [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                               [UIColor colorWithRed:205.0/255.0 green:255.0/255.0 blue:45.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
                                                               shadowObj, NSShadowAttributeName,
                                                               [UIFont fontWithName:@"Arial" size:18.0], NSFontAttributeName, nil]];

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

    }

答案 3 :(得分:0)

试用此代码

navigationController.navigationBar.tintColor = [UIColor colorWithRed:117/255.0f green:4/255.0f blue:32/255.0f alpha:1];