ios 显示/隐藏 barButtonItem

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32553197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:37:25  来源:igfitidea点击:

Show/Hide barButtonItem

iosswiftuibarbuttonitem

提问by Jessica

I'm trying to show/hide a UIBarButtonItem. I added a barButtonto the right side in the storyboard. Then in viewDidLoad, I made the rightBarButtonItemto nil. Later on, I set it to the buttonI added in the storyboard. Here's my code:

我正在尝试显示/隐藏UIBarButtonItem. 我barButtonstoryboard. 然后在viewDidLoad,我做了rightBarButtonItemto nil。后来,我将它设置为button我在storyboard. 这是我的代码:

// Right barButtonItem added in storybord:
@IBOutlet weak var deleteBarButton: UIBarButtonItem! 

// viewDidLoad
self.navigationItem.rightBarButtonItem = nil

// Later on...
self.navigationItem.rightBarButtonItem = self.deleteBarButton

When I set self.deleteBarButtonto the rightBarButtonItem, nothing happens. It doesn't show it. What am I doing wrong, and what's the correct/most efficient way to show/hide a barButtonItem?

当我设置self.deleteBarButton为 时rightBarButtonItem,没有任何反应。它不显示它。我做错了什么,显示/隐藏一个的正确/最有效的方法是barButtonItem什么?

Update

更新

I tried the following:

我尝试了以下方法:

self.deleteBarButton.hidden = true

But I get the following error:

但我收到以下错误:

UIBarButtonItemdoes not have a member named 'hidden'

UIBarButtonItem没有名为“隐藏”的成员

回答by Jessica

Just got the answer! All you have to do is create a strong IBOutlet, then you can do the following:

刚刚得到答案!您所要做的就是创建一个strong IBOutlet,然后您可以执行以下操作:

// viewDidLoad
self.navigationItem.rightBarButtonItem = nil

// Later on...
self.navigationItem.rightBarButtonItem = self.deleteBarButton

回答by Jake Chasan

Update 2

更新 2

You could just set the button's text to nothing:

您可以将按钮的文本设置为空:

self.deleteBarButton.title = "";

Update 1

更新 1

I would use the enabledproperty to illuminate the button as follows (although it does not completely make the button invisible, it allows the user to know it will not perform an action).

我将使用该enabled属性按如下方式照亮按钮(虽然它不会完全使按钮不可见,但它允许用户知道它不会执行操作)。

This can act as a variable to let you know that the button is hidden in your case:

这可以作为一个变量,让您知道按钮在您的案例中是隐藏的:

Illuminated: (place in ViewDidLoad)

点亮:(放置在 ViewDidLoad 中)

self.deleteBarButton.enabled = true;

Darkened: (place later on)

变暗:(稍后放置)

self.deleteBarButton.enabled = false;

Then I would add the following to make it completely disappear:

然后我会添加以下内容以使其完全消失:

self.navigationController?.navigationItem.rightBarButtonItem?.tintColor = UIColor.clearColor();

回答by mkz

Try to create your barButton manually in viewDidLoad and then show/hide your button.

尝试在 viewDidLoad 中手动创建 barButton,然后显示/隐藏您的按钮。

Code:

代码:

var barButton: UIBarButtonItem!

func viewDidLoad() {
    super.viewDidLoad()
    barButton = UIBarButtonItem(title: "Title", style: .Plain, target: self, action: Selector("target_function"))
    self.navigationItem.rightBarButtonItem = barButton
}

func someFunction() {
    self.navigationItem.rightBarButtonItem = nil
    // or
    self.navigationItem.rightBarButtonItem = barButton
}

回答by alitosuner

This one works perfect for me!!

这个非常适合我!!

self.navBar.title = ""
self.navBar.enabled = false