如何设置右侧导航栏按钮标题

时间:2014-07-21 16:47:14

标签: ios objective-c uinavigationcontroller uinavigationbar uinavigationitem

如何以编程方式设置右侧导航栏按钮的标题。我试过了

[self.navigationItem.rightBarButtonItem setTitle:@"Test"];

并且还尝试为按钮创建插座,然后尝试为其分配值。

self.rightBarButton.titleLabel.text = @"Test";

6 个答案:

答案 0 :(得分:8)

试试这个:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Test" style:UIBarButtonItemStylePlain target:target action:action];

答案 1 :(得分:5)

对于 Swift ,请使用此

self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Title", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("methodName"))

使用此设置标题的颜色,

self.navigationItem.rightBarButtonItem?.tintColor = UIColor.whiteColor()

答案 2 :(得分:4)

请试试这个,它有效:

    self.navigationItem.rightBarButtonItem.title = @"New Title";

答案 3 :(得分:1)

这是Swift 3的简单选项:

let rightButton = self.editButtonItem
rightButton.title = "My Button"
self.navigationItem.rightBarButtonItem = rightButton

答案 4 :(得分:0)

这适用于swift 3:

navigationItem.rightBarButtonItem?.title = "Test"

但按钮必须是自定义类型,否则它不起作用。

答案 5 :(得分:0)

单导航按钮

self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(yourFunctionWithObjcKeyword))

对于多个导航按钮

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]
  

注意:您必须将控制器嵌入UINavigationController中以使上述代码正常工作。对于UITableViewController和UICollectionViewController,此代码不起作用。