UINavigationBar后退按钮项目字体颜色不会设置

时间:2015-08-05 11:14:40

标签: ios objective-c uiviewcontroller uicolor

我在viewController上有一个导航栏,我可以启用/禁用。 问题是我无法获得UIBarButtonItem的字体,以便在视图最初加载后更改颜色,但后退箭头会发生变化。

我使用以下代码行停用 UINavigationBar 上的myViewController

    self.navigationController.navigationBar.userInteractionEnabled = NO;

我已为UIControlState 配置了AppDelegate.m UIBarButtonItem的{​​{1}}启用和禁用,但视图最初加载后字体没有任何变化。< / p>

在我的AppDelegate.m中,我有以下代码来设置UINavigationBar的字体和颜色:

// UIBarButtonItem styling
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor whiteColor];

// enabled
NSDictionary *enabledTextAttributeDictionary = @{NSForegroundColorAttributeName : [UIColor customCellHyperlinkColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:17.0]};

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:enabledTextAttributeDictionary forState:UIControlStateNormal];

// disabled
NSDictionary *disabledTextAttributeDictionary = @{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:17.0]};

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:disabledTextAttributeDictionary forState:UIControlStateDisabled];

// UINavigationBarTitle styling
NSDictionary *titleAttributeDictionary = @{NSForegroundColorAttributeName : [UIColor blackColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:@"GillSans" size:19.0]};

[[UINavigationBar appearanceWhenContainedIn:[UINavigationController class], nil]setTitleTextAttributes:titleAttributeDictionary];

我想我在UIControlStateNormal中配置了2个状态(UIControlStateDisabled / AppDelegate.m),除了启用/禁用navigationBar之外,不需要做任何事情。启用/禁用确实启用/禁用,但后退按钮标签上的颜色不会改变(尽管它最初设置为我在UIControlStateNormal中为AppDelegate设置的颜色。)

我尝试手动设置它,但backButtonItem上的标签保持蓝色,而左边的图标则呈浅灰色。我错过了什么(显而易见的)阻止我改变我的后退按钮字体的颜色?

2 个答案:

答案 0 :(得分:0)

我认为你需要使用

 [self.navigationController.navigationBar setBarTintColor:[UIColor lightGrayColor]];
 self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];
 [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : place your font here}];

而不是

 self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];

答案 1 :(得分:0)

我认为你正在寻找这个 在AppDelegate.m中添加此代码

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor clearColor];

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
 setTitleTextAttributes:
 @{NSForegroundColorAttributeName:[UIColor redColor],
   NSShadowAttributeName:shadow,
   NSFontAttributeName:[UIFont systemFontOfSize:13.0]
   }
 forState:UIControlStateNormal];