![](/img/trans.png)
[英]Set UIBarButtonItem target programmatically to previous view Controller
[英]Update UIBarButtonItem within view controller
我正在尝试从视图控制器更新UIBarButtonItem
色彩但没有任何改变。 我们不能访问UIBarButtonItem
使用视图控制器内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];
}];
我通过制作UIBarButtonItem的插座来尝试你的问题。 它工作正常。 您可以通过直接参考设置色调颜色。
这是代码
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
它可能对你有用。
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;
您也可以设置背景颜色和文本颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.