2011-04-29 57 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]]; 

我用随机数&颜色,你可以通过你自己的选择自定义颜色。希望它会有所帮助。