Button color in Navigation bar - iPhone

2019-01-23 17:43发布

How do you to set the tint of this yellow button to be gray? I have tried adding an image, but have had no luck.

Here is the screenshot:

alt text

Here is my current code:

- (id)initWithStyle:(UITableViewStyle)style {
    if (self = [super initWithStyle:style]) {

        UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
                                      initWithTitle:NSLocalizedString(@"Settings", @"")
                                      style:UIBarButtonItemStyleDone
                                      target:self
                                      action:@selector(GoToSettings)];
        [addButton setImage:[[UIImage imageNamed:@"bg_table.png"] retain]];

        self.navigationItem.rightBarButtonItem = addButton;
        self.navigationItem.hidesBackButton = TRUE;
        self.view.backgroundColor = [UIColor blackColor];
    }
    return self;
}

6条回答
我想做一个坏孩纸
2楼-- · 2019-01-23 17:48
 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
 [button setBackgroundImage:[UIImage imageNamed:@"button_main.png"]
                                       forState:UIControlStateNormal];
 [button addTarget:self action:@selector(GotoSettings)
              forControlEvents:UIControlEventTouchUpInside];
 button.frame = CGRectMake(x, y, x1, x2);

 UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:menu];
 self.navigationItem.rightBarButtonItem = menuButton;

 [button release];
 [menuButton release];
查看更多
\"骚年 ilove
3楼-- · 2019-01-23 17:49

From iOS 5.0 and on you can use:

    [[UINavigationBar appearance] setTintColor:[UIColor yellowColor]];
查看更多
forever°为你锁心
4楼-- · 2019-01-23 17:51

It is not possible without your custom image.

查看更多
迷人小祖宗
5楼-- · 2019-01-23 18:01
self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:0.1f green:0.66f blue:0.26f alpha:0.7];
查看更多
唯我独甜
6楼-- · 2019-01-23 18:02

when you set your image of the button in navigation bar, you may want to set UIImageRenderingModeAlwaysOriginal for it:

[addButton setImage:[[[UIImage imageNamed:@"bg_table.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] retain]];

here is the reference.

查看更多
够拽才男人
7楼-- · 2019-01-23 18:07

Actually it's possible without using image for buttons.

viewController.navigationController.navigationBar.tintColor = 
    [UIColor colorWithRed:0.16f green:0.36f blue:0.46 alpha:0.8];
查看更多
登录 后发表回答