1

我的包含视图的 viewDidLoad 中有以下代码:

// Now add the next button
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(self)];
self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];
self.navigationItem.rightBarButtonItem = nextButton;

UINavigationController 父级在 viewDidLoad 中有这个:

[super viewDidLoad];
// Do any additional setup after loading the view.

self.navigationBar.tintColor = [UIColor whiteColor];
self.navigationBar.barTintColor = [UIColor blackColor];

如何自定义 rightBar 按钮以更改背景颜色和文本颜色?

4

4 回答 4

19

分配按钮后,您必须在按钮上设置 tintColor 属性。

self.navigationItem.rightBarButtonItem = nextButton;
self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];

代替

self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];
self.navigationItem.rightBarButtonItem = nextButton;
于 2014-02-17T11:45:24.660 回答
5

如果您希望所有条形按钮的颜色相同,可以使用以下代码行:

self.window.tintColor = [UIColor redColor];

在您的应用委托中。

于 2013-09-30T07:34:36.767 回答
4

试试下面的代码,你可以设置按钮的颜色以及文本颜色

UIView *customBarButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];

UIButton *buttonDone = [UIButton buttonWithType:UIButtonTypeSystem];
[buttonDone addTarget:self action:@selector(doneButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

buttonDone.backgroundColor = [UIColor redColor];
   // or try this one
// buttonDone.tintColor = [UIColor redColor];

buttonDone.frame = CGRectMake(10, 0, 50, 30);

buttonDone.titleLabel.textColor = [UIColor colorWithRed:141.0/255.0 green:209.0/255.0 blue:205.0/255.0 alpha:1.0];
buttonDone.titleLabel.font = [UIFont fontWithName:@"SourceSansPro-Light" size:20.0f];
[buttonDone setTitle:@"Done" forState:UIControlStateNormal];
[customBarButtonView addSubview:buttonDone];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:customBarButtonView];
于 2014-02-17T12:13:26.713 回答
1

工作代码..试试这个。

UIImage *image = [UIImage imageNamed:@"Image.png"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
self.navigationItem.leftBarButtonItem = menuButton;
于 2015-10-23T11:49:56.413 回答