更改iOS 7中的酒吧button的颜色
我在我的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];
我怎样才能自定义右边的button来改变背景颜色和文字颜色?
分配button后,必须在button上设置tintColor属性。
self.navigationItem.rightBarButtonItem = nextButton; self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor];
代替
self.navigationItem.rightBarButtonItem.tintColor = [UIColor blueColor]; self.navigationItem.rightBarButtonItem = nextButton;
如果您希望所有条形button具有相同的颜色,则可以使用以下代码行:
self.window.tintColor = [UIColor redColor];
在你的应用程序委托。
尝试下面的代码,你可以设置button的颜色以及文本颜色
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];
工作代码..试试这个。
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;