How to change the text color on UINavigationBar button items

前端 未结 8 1608
梦如初夏
梦如初夏 2020-12-24 00:12

I\'ve got a UINavigationController and i\'ve changed it to white using the Tint property of the navigation bar in Interface Builder. But the text in buttons and the title is

相关标签:
8条回答
  • 2020-12-24 00:29

    you can change title appearance by adding label into the navigation bar like bellow.

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 35)];
    label.backgroundColor = [UIColor clearColor];
    label.textAlignment = UITextAlignmentCenter;
    label.textColor =[UIColor whiteColor];
    label.text=self.title;
    self.navigationItem.titleView = viewLabel;
    [label release]; 
    
    0 讨论(0)
  • 2020-12-24 00:32

    Here the clean way (relying on public API) to do this IMHO : you should use the titleView property of the navigationItem to apply it your own UILabel on which you will customize the textColor

    
    UILabel* lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
    lbl.textAlignment=UITextAlignmentCenter;
    lbl.backgroundColor=[UIColor clearColor];
    lbl.textColor=[UIColor colorWithRed:0.18 green:0.2 blue:0.56 alpha:1];
    theControllerOnWhichYouWantToHaveATitleWithYourOwnColor.navigationItem.titleView=lbl;
    [lbl release];
    
    

    0 讨论(0)
  • 2020-12-24 00:33
    for (id subView in theNavigationBar.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            [(UIButton *)subView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [(UIButton *)subView setTitleShadowColor:[UIColor clearColor] forState:UIControlStateNormal];
        }
    
    }
    
    0 讨论(0)
  • 2020-12-24 00:46

    Here's one way:

    [[theNavigationBar.subviews objectAtIndex:1] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [[theNavigationBar.subviews objectAtIndex:2] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    

    However, HUGE, caveat. This is highly likely to break on a future OS release and is not recommended.

    At the very least you should perform a lot of testing and make sure you your assumptions of the subview layout of the navigation bar are correct.

    0 讨论(0)
  • 2020-12-24 00:46

    Or you use your own button bar item subclass with a setter you specify, lus isn't iPhone os 3 suppose to exposé text color for EVERY button

    0 讨论(0)
  • 2020-12-24 00:48

    Can't you iterate the contents of the UINavigationBar view to find the buttons in the awakeFromNib?

    0 讨论(0)
提交回复
热议问题