簡體   English   中英

更改 UIBarButtonItem 的色調顏色

[英]Changing the Tint Color of UIBarButtonItem

我有一個使用 Storyboards 的項目,每當我使用 segue 推送視圖控制器時,動態創建的欄按鈕項始終為藍色。

在此處輸入圖片說明

它讓我發瘋。 因為這個對象是動態創建的,所以我不能在 IB 中設置它的顏色(就像我對以前的欄按鈕項目所做的那樣)。

我嘗試過的解決方案包括:

  1. 在接收者的viewDidLoad
  2. 在接收者的viewDidAppear

    self.navigationItem.backBarButtonItem.tintColor = [UIColor whiteColor];

  3. 當我看到這不太奏效時,我嘗試改為設置 leftBarButtonItem:

self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

  1. 當新視圖被調用時,以及在推送新視圖之前,我在我的應用程序委托中嘗試了以下代碼(我從其他 SO 答案中獲得):

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

我發現的所有谷歌答案都建議使用上面的代碼,但它對我來說根本不起作用。 也許 iOS 7 的外觀 API 有一些變化? 無論我如何或在何處嘗試將“Categorías”設置為白色,它始終是默認的藍色。

在 iOS 7 中,要設置應用程序中所有 barButtonItems 的顏色,請在 AppDelegate 中設置應用程序窗口上的tintColor屬性。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window.tintColor = [UIColor whiteColor];
    return YES;
}

Apple 的 iOS 7 UI 過渡指南中的更多詳細信息(特別是在“使用色調顏色”部分)。

***或***

根據一些評論,您還可以使用 UINavigationBar 外觀代理來實現這一點。 這將僅影響 UIBarButtonItems 的 tintColor,而不是在窗口上設置 tintColor 並影響該窗口的所有子視圖。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if([UINavigationBar conformsToProtocol:@protocol(UIAppearanceContainer)]) {
        [UINavigationBar appearance].tintColor = [UIColor whiteColor];
    }

    return YES;
}

我認為您正在尋找 UINavigationBar 的屬性。 嘗試設置self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

請參閱“導航欄的外觀”部分: https : //developer.apple.com/library/ios/documentation/userexperience/conceptual/UIKitUICatalog/UINavigationBar.html#//apple_ref/doc/uid/TP40012857-UINavigationBar-SW1

在 Swift 3.0 中

let navigationBarAppearnce = UINavigationBar.appearance()

導航欄的tintColor會影響背面指示器圖像、按鈕標題和按鈕圖像的顏色。

navigationBarAppearnce.barTintColor = UIColor(red: 0.180, green: 0.459, blue: 0.733, alpha: 1.00)

barTintColor 屬性影響條形本身的顏色

navigationBarAppearnce.tintColor = UIColor.white

最終代碼

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

 let navigationBarAppearnce = UINavigationBar.appearance()

 navigationBarAppearnce.barTintColor = UIColor(red: 0.180, green: 0.459, blue: 0.733, alpha: 1.00)

 navigationBarAppearnce.tintColor = UIColor.white

 navigationBarAppearnce.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

 //Change status bar color
 UIApplication.shared.statusBarStyle = .lightContent

 return true
}

在此處輸入圖片說明

斯威夫特 5

barButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)

要更改導航欄中特定項目(例如按鈕)的顏色:在 Objective-C 中

myButton.tintColor = [UIColor redColor];

在 iOS 8 中,如果您出於某種目的更改了 UIView 色調顏色,例如為 UIAlertView 品牌化,UIToolBar 中 UIBarButtonItem 的色調顏色也會以這種方式更改。 要解決此問題,只需編寫此代碼

[UIView appearance].tintColor = SOME_COLOR;
[UIView appearanceWhenContainedIn:[UIToolbar class], nil].tintColor = BLACK_COLOR;

對於 UINavigationBar 中的 UIBarButtonItem 色調顏色使用標准方法

[UINavigationBar appearance].tintColor = BLACK_COLOR;
UITabBar.appearance().tintColor = UIColor.yellowColor()

這對我有用

AddBarButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)

暫無
暫無

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

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