How do I programatically set my right navigation bar button's title. I've tried
[self.navigationItem.rightBarButtonItem setTitle:@"Test"];
and have also tried creating an outlet for the button and then trying to assign it a value.
self.rightBarButton.titleLabel.text = @"Test";
尝试这个:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Test" style:UIBarButtonItemStylePlain target:target action:action];
For Swift use this,
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Title", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("methodName"))
Set Color of Title using this,
self.navigationItem.rightBarButtonItem?.tintColor = UIColor.whiteColor()
请尝试一下,它可以工作:
self.navigationItem.rightBarButtonItem.title = @"New Title";
This is the simple option for Swift 3:
let rightButton = self.editButtonItem
rightButton.title = "My Button"
self.navigationItem.rightBarButtonItem = rightButton
This works for swift 3:
navigationItem.rightBarButtonItem?.title = "Test"
But the button has to be of custom type, otherwise it doesn't work.
For Single Navigation Button
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(yourFunctionWithObjcKeyword))
For Multiple Navigation Buttons
let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(yourFunctionWithObjcKeyword))
let edit = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(yourFunctionWithObjcKeyword))
self.navigationItem.rightBarButtonItems = [add, edit]
Note: you must embed your controller inside UINavigationController to make above code work. In case of UITableViewController and UICollectionViewController, this code doesn't work.
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.