![](/img/trans.png)
[英]Set background color (not Text Color) of Back button in UINavigationBar IOS
[英]UINavigationBar back button item font color won't set
我在viewController上有一个导航栏,可以启用/禁用它。 问题是 ,尽管后退箭头会更改,但在最初加载视图后, 我无法让 UIBarButtonItem
的字体更改颜色。
我使用以下代码行禁用myViewController
上的UINavigationBar
:
self.navigationController.navigationBar.userInteractionEnabled = NO;
我在AppDelegate.m
为UIBarButtonItem
启用和禁用了UIControlState
配置为启用和禁用,但是在初始加载视图后,字体没有任何反应。
在我的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];
我以为,因为我在AppDelegate.m
配置了两种状态( UIControlStateNormal
/ UIControlStateDisabled
),所以除了启用/禁用navigationBar
之外,无需执行其他任何操作。 启用/禁用确实确实启用/禁用,但是后退按钮标签上的颜色没有更改(尽管它最初设置为我为AppDelegate
UIControlStateNormal
设置的颜色)。
我尝试手动设置它,但是backButtonItem上的标签保持蓝色,而其左侧的图标则变为浅灰色。 我缺少什么(显而易见的东西)而无法更改后退按钮字体的颜色?
我认为你需要使用
[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];
我认为您正在寻找此代码在您的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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.