【问题标题】:Back button text will not change tint后退按钮文本不会改变色调
【发布时间】:2015-05-20 01:38:50
【问题描述】:

我正在尝试更改某些视图的后退按钮文本颜色。我可以更改箭头颜色,但文本不会更改色调。我试过了:

NSDictionary *dictionary = @{NSForegroundColorAttributeName : [UIColor redColor], NSShadowAttributeName : shadow, NSFontAttributeName : font};

[self.navigationItem.backBarButtonItem setTitleTextAttributes:dictionary forState:UIControlStateNormal];

我也试过了:

NSString *backText = self.navigationItem.backBarButtonItem.title;
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:backText style:UIBarButtonItemStylePlain target:nil action:nil];
NSDictionary *dictionary = @{NSForegroundColorAttributeName : [UIColor redColor], NSShadowAttributeName : shadow, NSFontAttributeName : font};
[backButton setTitleTextAttributes:dictionary forState:UIControlStateNormal];
self.navigationItem.backBarButtonItem = backButton;

我在 Storyboard 中设置了全局色调。它应该覆盖它吗?

我怎样才能改变文字?

【问题讨论】:

    标签: ios uinavigationcontroller uibarbuttonitem


    【解决方案1】:

    您是否尝试过更改导航栏的色调?在情节提要中,转到 UINavigationController。在大纲中,选择导航栏。然后在 Attributes Inspector 中,更改 Tint 颜色(在 View 部分下)。这会更改默认后退按钮的颜色。

    【讨论】:

    • 我刚刚尝试过,它确实有效,但我希望在同一个导航堆栈中的不同屏幕上使用不同的后退按钮文本颜色。箭头会改变,但文字不会改变。
    • viewWillAppearself.navigationController.navigationBar.tintColor = <Insert UIColor>;
    • 试过了,没用。我已经尝试了我能想到的一切。除了我在情节提要中的全局色调外,我没有设置色调,但这应该会覆盖它。
    • 如果情节提要中一切正常,但当您以编程方式进行更改时却不行,则几乎没有什么需要检查的。 1) 当您转到 Storyboard 中的 Identity Inspector 时显示的 UIViewController 的类应该与您放置 self.navigationController.navigationBar.tintColor 代码的 UIViewController 子类匹配。您还可以在此行上设置断点并确保正在调用它。 2)您是否使用 UINavigationController 的子类。这本身可能有一个setTint 导致麻烦。 3)你在任何地方使用[UINavigationBar appearance]吗?
    • 4) 您是否在任何地方添加 UINavigationBar 对象?还是您只使用 UINavigationController 附带的默认值?
    【解决方案2】:

    你试过了吗?

    [self.navigationController.navigationBar setTintColor:[UIColor redColor]];
    

    您可以在-(BOOL)application:application didFinishLaunchingWithOptions:launchOptions 下的AppDelegate.m 下添加该行,在我的情况下,我出于某种原因以编程方式使用 uinavigationcontroller..

    所以我的看起来像这样..

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        //..codes
         [self.navController.navigationBar setTintColor:[UIColor yourColor]];//fontColor
         //..codes
    }
    

    storyboard 中,您可能会在 View > Utilities > Show attribute inspector > View > Tint 下找到它。在此处更改颜色,仅此而已..

    嘿!我试过你的代码! :) 它对我来说很好用.. 我认为您只是放错了代码...

    不要将代码放在当前视图控制器中-viewDidLoad 在它之前的视图控制器中添加它..

    类似这样的.. //firstViewController.m

    - (void)viewDidLoad
    {
        //place your code here..
    }
    

    你会在//secondViewController.m看到效果

    【讨论】:

    • 我不认为你想把它放在 viewDidLoad 中,因为如果你在视图控制器之间来回移动,你想在每次视图出现时更新颜色。
    • 哦是的..哈哈..我只是从我的项目中复制粘贴的代码..每个视图控制器具有不同的颜色...我会编辑它.. :)
    • 是的,当我把它放在 prepareForSegue 中时它可以工作。我想知道是否存在全局色调颜色并在情节提要中继承的错误。我正在尝试玩一些东西。
    • 哈哈.. 好吧,这对我来说听起来很有趣.. 祝你玩得好对你正在做的事情很感兴趣.. 干杯!
    【解决方案3】:

    所以答案是将此代码放在导航堆栈中上一个类的 prepareForSegue 中。

    NSString *backText = self.navigationItem.backBarButtonItem.title;
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:backText style:UIBarButtonItemStylePlain target:nil action:nil];
    NSDictionary *dictionary = @{NSForegroundColorAttributeName : <My Color Here>, NSShadowAttributeName : shadow, NSFontAttributeName : font};
    [backButton setTitleTextAttributes:dictionary forState:UIControlStateNormal];
    self.navigationItem.backBarButtonItem = backButton;
    

    这似乎是一个非常糟糕的方法。故事板中的继承是否有错误?我会努力学习更多。

    【讨论】:

      猜你喜欢
      • 2012-09-06
      • 2021-12-16
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      相关资源
      最近更新 更多