繁体   English   中英

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

[英]UINavigationBar back button item font color won't set

我在viewController上有一个导航栏,可以启用/禁用它。 问题是 ,尽管后退箭头会更改,但在最初加载视图后, 我无法让 UIBarButtonItem字体更改颜色。

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

    self.navigationController.navigationBar.userInteractionEnabled = NO;

我在AppDelegate.mUIBarButtonItem启用和禁用了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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM