2011-04-29 19 views
3

有沒有辦法將self.navigationItem.prompt顏色設置爲White?目前它是黑色的。iPhone/Objective-C - 更改默認的「navigationItem.prompt」顏色

這是我如何設置的那一刻我的導航欄顏色:

// Set top navigation bar. 
UINavigationBar *bar = [navController navigationBar]; 
[bar setTintColor:[UIColor colorWithRed:180.0/255.0 green:25.0/255.0 blue:34.0/255.0 alpha:1]]; 
UIImageView *navBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
[navBar release]; 

..和這是我怎麼設置我的navigationItem.prompt

self.navigationItem.prompt = @"Tap image for more options."; 
+0

你這是什麼意思navigationBar.prompt? – saadnib 2011-04-29 07:39:59

+0

navigationItem.prompt not navigationBar.prompt – fuzz 2011-04-29 16:19:54

+1

[Change textColor of UINavigationBar「prompt」?](http:// stackoverflow。com/questions/1491345/change-textcolor-of-uinavigationbar-prompt) – Moshe 2011-04-29 16:23:39

回答

0

斯威夫特:

 self.navigationController?.navigationBar.titleTextAttributes = 
       [NSForegroundColorAttributeName : UIColor.white] 
     self.navigationController?.navigationBar.barTintColor = UIColor.red 
+0

至少在iOS 11中提示顏色仍然是黑色 – lostintranslation 2017-11-08 16:19:59

1

嘗試這種方式。

navigationhBar.tintColor = [UIColor colorWithRed:0.4039216041564941 green:0.6470588445663452 blue:0.062745101749897 alpha:1];

+0

首先,使navigationBar成爲綠色,其次是所有文本仍然是黑色。我所要求的是navigationBar保持紅色並將navigationBar.prompt顏色更改爲White。 – fuzz 2011-04-29 04:01:41

+0

@Anand謝謝你的回答。 – fuzz 2011-04-29 04:02:10

+0

檢查紅色的RGB值並設置它。 – Anand 2011-04-29 04:12:40

3

恐怕提示顏色是硬編碼,而不是定製的。

我管理使用以下(棘手)破解,即更換提示來定製提示:

  • 設置navigationItem.prompt = @""迫使的導航欄顯示一個空的提示
  • 與設置navigationItem.titleView
    • 一個UILabel或任何你想更換標題,裏面,作爲一個子視圖,更換提示:
      • 另一個的UILabel作爲提示,風格,只要你喜歡
      • 該標籤被x爲中心,並與AY = -31px偏移量(這就是正常的提示是)
      • 確保titleview的具有.clipsToBounds = NO所以自定義提示drawed正常

我通過重寫UIViewController.setTitle:的東西,如(這是行不通/爲的是建立應用這個爲我所有的控制器,這是一個比較複雜的合作的快速提取德,但示出了邏輯)

- (void)setTitle:(NSString *)title { 
    [super setTitle:title]; 
    UILabel *titleLabel = ... build the titleLabel 
    titleLabel.text = title; 
    if (self.navigationItem.prompt) { 
    UILabel *promptLabel = ... build the promptLabel 
    promptLabel.text = self.navigationItem.prompt; 
    self.navigationItem.prompt = @""; 
    promptLabel.centerX = titleLabel.width/2; 
    promptLabel.top = -31; 
    [titleLabel addSubview:promptLabel]; 
    titleLabel.clipsToBounds = NO; 
    } 
    self.navigationItem.titleView = titleLabel; 
} 
+0

如果我們試圖覆蓋UINavigationItem類別中的setPrompt,這將如何工作?我似乎無法得到它的工作。我想在按下某個按鈕時顯示提示,並非總是如此。 – 2012-02-29 22:30:42

+0

從內存(我可能是錯誤的)提示只有當控制器被推入導航控制器時,然後設置導航item.prompt後沒有任何影響。 – 2012-03-02 09:13:16

7

及其可能現在在IOS 5或更高。試試這個代碼。

[[UINavigationBar appearance] setTitleTextAttributes: 
     [NSDictionary dictionaryWithObjectsAndKeys: 
     [UIColor whiteColor], UITextAttributeTextColor, nil]]; 
1

在iOs 5中,我更改了UINavigationBar的外觀。您可以更改提示的顏色與下面的代碼

[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor], UITextAttributeTextColor, [UIColor colorWithRed:10 green:20 blue:30 alpha:1.0f], UITextAttributeTextShadowColor, [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset, nil]]; 

我用隨機數&顏色,你可以通過你自己的選擇自定義顏色。希望它會有所幫助。