简体   繁体   English

无法为自定义栏按钮设置标题?

[英]Unable to set title for custom bar button?

I have added one custom bar button in my app.I want to set it's title but i am not able to set its title.I am using below code in swift 3.Please advice the best solution. 我在应用程序中添加了一个自定义栏按钮。我想设置它的标题,但我无法设置它的标题。我正在快速3中使用以下代码,请提出最佳解决方案。

let customButton=UIButton(frame: CGRect(x: 0, y: 0, width: 60, height:30))
customButton.backgroundColor=Constant.Color.customBlue
customButton.titleLabel?.text = "Add"
customButton.titleLabel?.textColor=UIColor.white
let barButton = UIBarButtonItem(customView:customButton)
barButton.title="Add"
barButton.tintColor=UIColor.white
self.navigationItem.rightBarButtonItem=barButton

您需要设置标题如下:

 customButton?.setTitle("Add", for:.normal)

您必须使用此:

[customButton setTitle:@"Add" forState:UIControlStateNormal];

Try This code: 试试下面的代码:

Note: This is a better approach to apply barButton to your navigationBar with action. 注:这是barButton适用于您的导航栏用行动一个更好的办法。

 navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action:  #selector(addTapped))

 func addTapped() {
    //Do your stuff here. After Add button pressed
    print("Add Button Pressed")
 }

If you still want to use your code.Add the following code to set title to your button as KKRocks answer. 如果仍要使用代码,请添加以下代码以将标题设置为KKRocks答案。

  customButton?.setTitle("Add", for:.normal)

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

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