簡體   English   中英

更改UINavigationBar的顏色

[英]Change in UINavigationBar's color

我正在使用iOS 7應用。 我的導航欄以前的外觀如下圖:

在此處輸入圖片說明

但是添加了這段代碼之后

self.edgesForExtendedLayout = UIRectEdgeNone;

導航車的顏色變暗,如下圖所示: 在此處輸入圖片說明

在保持上面的代碼的同時,我們如何讓導航欄保持如第一張圖片一樣明亮?

默認情況下,導航欄的半透明屬性設置為YES。
此外,所有導航欄都有系統模糊。 在此設置下,iOS 7傾向於降低條形的顏色。

差異半透明設置

差異半透明設置 設定色調顏色 設定色調顏色

關閉半透明設置

關閉半透明設置

將此代碼放在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]];

}

試試看

對於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;
}

在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]];

    }

試試這個代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM