简体   繁体   中英

Button color in Navigation bar - iPhone

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:

替代文字

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;
}
self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithRed:0.1f green:0.66f blue:0.26f alpha:0.7];

从iOS 5.0及以上版本,您可以使用:

    [[UINavigationBar appearance] setTintColor:[UIColor yellowColor]];
 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];

没有您的自定义图像是不可能的。

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];

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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