I'm trying to update UIBarButtonItem
tint color from a view controller but nothing has changed. Can't we access the UIBarButtonItem
within view controller using self.navigationItem.rightBarButtonItem
?
[UIView animateWithDuration:.8 delay:0.5 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
//button.alpha = .01; //don't animate alpha to 0, otherwise you won't be able to interact with it
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
} completion:^(BOOL finished) {
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
}];
I have tried your problem by making outlet of UIBarButtonItem. It is working fine. You can set the tint color by direct refrence.
Here is the code
class ViewController: UIViewController {
@IBOutlet weak var addItem: UIBarButtonItem!
var username = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func addItem(_ sender: UIBarButtonItem) {
UIView.animate(withDuration: 20) {
self.addItem.tintColor = UIColor.red
}
}
}
let addButton = UIButton(type: .custom)
addButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
addButton.tintColor = UIColor.gray
let addMoreItem = UIBarButtonItem(customView: addButton)
self.navigationItem.rightBarButtonItem = addMoreItem
it may be work for you.
UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[btnback setTintColor:[UIColor whiteColor]];
[btnBack addTarget:self action:@selector(onClickBtnBack:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.rightBarButtonItem = barButtonItem;
also you can set background color and text color.
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.