如何隐藏/显示导航栏中的右侧按钮
我需要隐藏导航栏中的右侧按钮,然后在用户选择某些选项后取消隐藏它。
不幸的是,以下方法不起作用:
NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES; // FOO CODE
有办法吗?
I need to hide the right button in the Navigation Bar, then unhide it after the user selects some options.
Unfortunately, the following doesn't work:
NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES; // FOO CODE
Is there a way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
这个答案必须归功于学习者,答案来自这个问题:
按需隐藏和显示左侧导航栏按钮iOS-7
这就是答案,要简单得多。
然后,您只需在
(IBAction)
中引用您需要的方法,如下所示:我尝试了所有其他方法,但没有一个有效,甚至将我的按钮引用为
@property (... ) UIBarButtonItem....
在我发现这个之前没有任何效果。Credit has to go to learner for this answer which the answer is from this question:
hide and show left navigation bar button on demand in iOS-7
This is the answer, which is far more simple.
Then you just reference the method where you require it like within an
(IBAction)
like so:I tried all other methods and none worked, even referencing my button as a
@property (...) UIBarButtonItem....
and nothing worked until I found this.SWIFT 2.2
在 swift 2.2 中 self.navigationItem 不起作用。相反,创建 NavigationItem 的出口(我将其命名为“nav”)并使用它。
另外,以下建议对使用 Xcode 7.3 和 swift 2.2 的我不起作用,
因此我使用了上面 @Matt J 的想法,如下所示(我左边有 2 个项目):
为导航栏中的项目创建出口,并存储它们的变量
将项目保存在 viewDidLoad() 中
要隐藏,请将它们设置为 nil
要显示它们
SWIFT 2.2
In swift 2.2 self.navigationItem does not work. Instead create an outlet of the NavigationItem (I named it below "nav") and use it.
Also the following suggestion did not work for me using Xcode 7.3 and swift 2.2
So I used the idea of @Matt J above as follows (I have 2 items on the left):
Create outlets for the items in the navigation bar and variables to store them
Save the items in viewDidLoad()
To HIDE set them to nil
To SHOW them
显示:
隐藏:
Show:
Hide:
我的解决方案:
My solution:
斯威夫特2:
隐藏:
显示:
Swift 2:
Hide:
Show:
您可以使用下面的代码:
You can use below code:
对于 swift 5 隐藏 rightBarButtonItem
For swift 5 to hide rightBarButtonItem
在swift 4中,我有一个显示/隐藏右侧或左侧按钮的技巧:
第1步:在视图控制器中创建一个IBOutlet按钮:
第2步: > 创建隐藏按钮函数:
第 3 步: 创建显示按钮函数:
第 4 步:只需调用您想要的函数,使用
hideNavigationButton()
隐藏,showNavigationButton()
显示按钮。问候!
In swift 4 I has a trick to show / hide right or left button:
Step 1: Create a IBOutlet button in view controller:
Step 2: Create Hide button function:
Step 3: Create Show button function:
Step 4: Just call the functions that you want, use
hideNavigationButton()
to hide, andshowNavigationButton()
to show the button.Regards!
显示:
隐藏:
Show:
Hide:
xxxButton
(请打开 Assistant Editor,按住 Control+将 xxx 按钮拖动到 YourViewController 类作为插座“xxxButton”)。
或者您可以使用类似
let xxxButton = navigationBar.buttons[1]
Hide
<代码>
xxxButton.customView = UIView()
或者
<代码>
navigationItem.rightBarButtonItems?.remove(位于:(navigationItem.rightBarButtonItems?.index(of:xxxButton)!)!)
隐藏
显示
<代码>
xxxButton.customView = nil
或者
<代码>
navigationItem.rightBarButtonItems?.insert(newElement: xxxButton, at:SOME_INDEX)
有帮助。
xxxButton
(please open Assistant Editor, Control+Drag xxx button to YourViewController class as outlet "xxxButton").
or you can use something like
let xxxButton = navigationBar.buttons[1]
Hide
xxxButton.customView = UIView()
or
navigationItem.rightBarButtonItems?.remove(at: (navigationItem.rightBarButtonItems?.index(of:xxxButton)!)!)
Show
xxxButton.customView = nil
or
navigationItem.rightBarButtonItems?.insert(newElement: xxxButton, at:SOME_INDEX)
Hope helpful.
隐藏:
To hide:
首先将标题设置为空,然后在 swlwction 后再次设置。
Set the the title to empty first and after swlwction just set again.
通过将引用设置为 nil 来隐藏该按钮,但是如果您想稍后恢复它,则需要保留它的副本,以便可以重新分配它。
就我个人而言,在我的应用程序中,我将导航按钮设置为 @properties,以便我可以删除和删除内容。随意重新创建它们,例如:
Hide the button by setting the reference to nil, however if you want to restore it later, you'll need to hang onto a copy of it so you can reassign it.
Personally, in my apps I make my nav buttons into @properties, so that I can trash & recreate them at will, so something like:
对于斯威夫特 3
For Swift 3
显示:
隐藏:
您甚至可以设置其显示/隐藏的动画
Show:
Hide:
You can even animate its showing/hiding
将引用设置为 nil:
还要确保在 navController 当前显示的控制器中调用此函数,而不是在 navController 本身中调用此函数。
Set reference to nil:
Also be sure to call this in the controller currently shown by the navController, not for the navController itself.
如果右侧只有一个栏按钮项,则可以使用此一项,
假设您的右侧有多个栏按钮,例如,假设您的导航项的右侧有两个栏按钮项目(搜索按钮和过滤按钮)。现在右栏按钮项目是
并且您必须仅隐藏搜索按钮,您可以使用类似,
现在发生的事情是,您可以从导航项中完全隐藏搜索按钮,并且过滤项出现在搜索项的位置
If you have only one bar button item in the right side you can use this one,
Suppose if you have multiple bar button in the right side, for example suppose you have two bar button items(search button and filter button) in the right side of your navigation item. Now the right bar button items are
and you have to hide only search button, you can use like,
Now what happening is, you can completely hide the search button from the navigation item and the filter item comes in the place of search item
这是马特针对故事板和故事板更新的解决方案。弧。请记住,IBOutlet 默认情况下是 __weak,因此您需要将其更改为强,以免过早发布。
Here's Matt's solution updated for Storyboards & ARC. Remember, IBOutlets are __weak by default, so you need to change that to strong for it not to be released too early.