简体   繁体   English

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

[英]How to set right navigation bar button title

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, 对于Swift来说,

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: 这是Swift 3的简单选项:

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

This works for swift 3: 这适用于快速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. 注意:您必须将控制器嵌入UINavigationController中才能使上述代码正常工作。 In case of UITableViewController and UICollectionViewController, this code doesn't work. 对于UITableViewController和UICollectionViewController,此代码不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM