如何隐藏/显示导航栏中的右侧按钮

发布于 2024-10-30 19:16:16 字数 178 浏览 8 评论 0原文

我需要隐藏导航栏中的右侧按钮,然后在用户选择某些选项后取消隐藏它。

不幸的是,以下方法不起作用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

帝王念 2024-11-06 19:16:17

这个答案必须归功于学习者,答案来自这个问题:

按需隐藏和显示左侧导航栏按钮iOS-7

这就是答案,要简单得多。

//hide and reveal bar buttons
-(void) hideAndDisableLeftNavigationItem
{
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor clearColor]];
    [self.navigationItem.leftBarButtonItem setEnabled:NO];
}

-(void) showAndEnableLeftNavigationItem
{
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor blueColor]];
    [self.navigationItem.leftBarButtonItem setEnabled:YES];
}

然后,您只需在 (IBAction) 中引用您需要的方法,如下所示:

[self hideAndDisableLeftNavigationItem];//[self showAndEnableLeftNavigationItem]; to show again

我尝试了所有其他方法,但没有一个有效,甚至将我的按钮引用为 @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.

//hide and reveal bar buttons
-(void) hideAndDisableLeftNavigationItem
{
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor clearColor]];
    [self.navigationItem.leftBarButtonItem setEnabled:NO];
}

-(void) showAndEnableLeftNavigationItem
{
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor blueColor]];
    [self.navigationItem.leftBarButtonItem setEnabled:YES];
}

Then you just reference the method where you require it like within an (IBAction) like so:

[self hideAndDisableLeftNavigationItem];//[self showAndEnableLeftNavigationItem]; to show again

I tried all other methods and none worked, even referencing my button as a @property (...) UIBarButtonItem.... and nothing worked until I found this.

唠甜嗑 2024-11-06 19:16:17

SWIFT 2.2

在 swift 2.2 中 self.navigationItem 不起作用。相反,创建 NavigationItem 的出口(我将其命名为“nav”)并使用它。

另外,以下建议对使用 Xcode 7.3 和 swift 2.2 的我不起作用,

 nav.leftBarButtonItem?.customView?.hidden = true

因此我使用了上面 @Matt J 的想法,如下所示(我左边有 2 个项目):

  1. 为导航栏中的项目创建出口,并存储它们的变量

    @IBOutlet 弱变量设置项:UIBarButtonItem!
    @IBOutlet 弱 var logoItem:UIBarButtonItem!
    
    var sav_settingItem: UIBarButtonItem = UIBarButtonItem()
    var sav_logoItem: UIBarButtonItem = UIBarButtonItem()
    
  2. 将项目保存在 viewDidLoad() 中

    sav_settingItem = nav.leftBarButtonItems![0]
    sav_logoItem = nav.leftBarButtonItems![1]
    
  3. 要隐藏,请将它们设置为 nil

    nav.leftBarButtonItem = nil
    nav.leftBarButtonItem = nil
    
  4. 要显示它们

    if (nav.leftBarButtonItem == nil) {
        nav.leftBarButtonItem = sav_settingItem
        nav.leftBarButtonItems?.append(sav_logoItem)
        返回
    }
    

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

 nav.leftBarButtonItem?.customView?.hidden = true

So I used the idea of @Matt J above as follows (I have 2 items on the left):

  1. Create outlets for the items in the navigation bar and variables to store them

    @IBOutlet weak var settingItem: UIBarButtonItem!
    @IBOutlet weak var logoItem: UIBarButtonItem!
    
    var sav_settingItem: UIBarButtonItem = UIBarButtonItem()
    var sav_logoItem: UIBarButtonItem = UIBarButtonItem()
    
  2. Save the items in viewDidLoad()

    sav_settingItem = nav.leftBarButtonItems![0]
    sav_logoItem = nav.leftBarButtonItems![1]
    
  3. To HIDE set them to nil

    nav.leftBarButtonItem = nil
    nav.leftBarButtonItem = nil
    
  4. To SHOW them

    if (nav.leftBarButtonItem == nil) {
        nav.leftBarButtonItem = sav_settingItem
        nav.leftBarButtonItems?.append(sav_logoItem)
        return
    }
    
挥剑断情 2024-11-06 19:16:17

显示:

[self.navigationItem.rightBarButtonItem.customView setHidden:NO];

隐藏:

[self.navigationItem.rightBarButtonItem.customView setHidden:YES];

Show:

[self.navigationItem.rightBarButtonItem.customView setHidden:NO];

Hide:

[self.navigationItem.rightBarButtonItem.customView setHidden:YES];
梦中楼上月下 2024-11-06 19:16:17

我的解决方案:

self.navigationItem.rightBarButtonItem.customView.hidden=NO;

My solution:

self.navigationItem.rightBarButtonItem.customView.hidden=NO;
故乡的云 2024-11-06 19:16:17

斯威夫特2

诡计!

隐藏:

if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
        btn.enabled = false
        btn.title = ""
}

显示:

if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
        btn.enabled = true
        btn.title = "ButtonName"
}

Swift 2:

Trick!

Hide:

if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
        btn.enabled = false
        btn.title = ""
}

Show:

if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
        btn.enabled = true
        btn.title = "ButtonName"
}
暖心男生 2024-11-06 19:16:17

您可以使用下面的代码:

    self.navigationItem.rightBarButtonItem?.image = nil
    self.navigationItem.rightBarButtonItem?.isEnabled = false

You can use below code:

    self.navigationItem.rightBarButtonItem?.image = nil
    self.navigationItem.rightBarButtonItem?.isEnabled = false
烧了回忆取暖 2024-11-06 19:16:17

对于 swift 5 隐藏 rightBarButtonItem

self.navigationItem.rightBarButtonItem?.customView?.isHidden = true

For swift 5 to hide rightBarButtonItem

self.navigationItem.rightBarButtonItem?.customView?.isHidden = true
⒈起吃苦の倖褔 2024-11-06 19:16:17

swift 4中,我有一个显示/隐藏右侧或左侧按钮的技巧:

第1步:在视图控制器中创建一个IBOutlet按钮:

@IBOutlet var navigationItemButton: UIBarButtonItem!

第2步: > 创建隐藏按钮函数:

func hideNavigationButton() {
    navigationItemButton.isEnabled = false
    navigationItemButton.tintColor = UIColor.clear
}

第 3 步: 创建显示按钮函数:

func showNavigationButton() {
    navigationItemButton.isEnabled = true
    navigationItemButton.tintColor = UIColor.white
}

第 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:

@IBOutlet var navigationItemButton: UIBarButtonItem!

Step 2: Create Hide button function:

func hideNavigationButton() {
    navigationItemButton.isEnabled = false
    navigationItemButton.tintColor = UIColor.clear
}

Step 3: Create Show button function:

func showNavigationButton() {
    navigationItemButton.isEnabled = true
    navigationItemButton.tintColor = UIColor.white
}

Step 4: Just call the functions that you want, use hideNavigationButton() to hide, and showNavigationButton() to show the button.

Regards!

因为看清所以看轻 2024-11-06 19:16:17

显示:

//set navigationItem tint color white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];

隐藏:

//set navigationItem tint clear white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor clearColor];

Show:

//set navigationItem tint color white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];

Hide:

//set navigationItem tint clear white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor clearColor];
不语却知心 2024-11-06 19:16:17
  1. 假设您可以将特定的栏按钮引用为变量 xxxButton

(请打开 Assistant Editor,按住 Control+将 xxx 按钮拖动到 YourViewController 类作为插座“xxxButton”)。

或者您可以使用类似 let xxxButton = navigationBar.buttons[1]

  1. Hide
    <代码>
    xxxButton.customView = UIView()

    或者
    <代码>
    navigationItem.rightBarButtonItems?.remove(位于:(navigationItem.rightBarButtonItems?.index(of:xxxButton)!)!)
    隐藏

  2. 显示
    <代码>
    xxxButton.customView = nil

    或者
    <代码>
    navigationItem.rightBarButtonItems?.insert(newElement: xxxButton, at:SOME_INDEX)

有帮助。

  1. Assume you can reference the specific bar button as variable 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]

  1. Hide

    xxxButton.customView = UIView()

    or

    navigationItem.rightBarButtonItems?.remove(at: (navigationItem.rightBarButtonItems?.index(of:xxxButton)!)!)

  2. Show

    xxxButton.customView = nil

    or

    navigationItem.rightBarButtonItems?.insert(newElement: xxxButton, at:SOME_INDEX)

Hope helpful.

冷情 2024-11-06 19:16:17

隐藏:

if let topItem = self.navigationController?.navigationBar.topItem {
    topItem.rightBarButtonItem = nil
}

To hide:

if let topItem = self.navigationController?.navigationBar.topItem {
    topItem.rightBarButtonItem = nil
}
痴情 2024-11-06 19:16:17

首先将标题设置为空,然后在 swlwction 后再次设置。

Set the the title to empty first and after swlwction just set again.

爱要勇敢去追 2024-11-06 19:16:16

通过将引用设置为 nil 来隐藏该按钮,但是如果您想稍后恢复它,则需要保留它的副本,以便可以重新分配它。

UIBarButtonItem *oldButton = self.navigationItem.rightBarButtonItem;
[oldButton retain];
self.navigationItem.rightBarButtonItem = nil;

//... later
self.navigationItem.rightBarButtonItem = oldButton;
[oldButton release];

就我个人而言,在我的应用程序中,我将导航按钮设置为 @properties,以便我可以删除和删除内容。随意重新创建它们,例如:

//mycontroller.h
UIBarButtonItem *rightNavButton;
@property (nonatomic, retain) UIBarButtonItem *rightNavButton;

//mycontroller.m
@synthesize rightNavButton;
- (UIBarButtonItem *)rightNavButton {
    if (!rightNavButton) {
        rightNavButton = [[UIBarButtonItem alloc] init];
        //configure the button here
    }
    return rightNavButton;
}

//later, in your code to show/hide the button:
self.navigationItem.rightBarButtonItem = self.rightNavButton;

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.

UIBarButtonItem *oldButton = self.navigationItem.rightBarButtonItem;
[oldButton retain];
self.navigationItem.rightBarButtonItem = nil;

//... later
self.navigationItem.rightBarButtonItem = oldButton;
[oldButton release];

Personally, in my apps I make my nav buttons into @properties, so that I can trash & recreate them at will, so something like:

//mycontroller.h
UIBarButtonItem *rightNavButton;
@property (nonatomic, retain) UIBarButtonItem *rightNavButton;

//mycontroller.m
@synthesize rightNavButton;
- (UIBarButtonItem *)rightNavButton {
    if (!rightNavButton) {
        rightNavButton = [[UIBarButtonItem alloc] init];
        //configure the button here
    }
    return rightNavButton;
}

//later, in your code to show/hide the button:
self.navigationItem.rightBarButtonItem = self.rightNavButton;
泅人 2024-11-06 19:16:16

对于斯威夫特 3

if let button = self.navigationItem.rightBarButtonItem {
                    button.isEnabled = false
                    button.tintColor = UIColor.clear
                }`

For Swift 3

if let button = self.navigationItem.rightBarButtonItem {
                    button.isEnabled = false
                    button.tintColor = UIColor.clear
                }`
人事已非 2024-11-06 19:16:16

显示:

[self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];

隐藏:

[self.navigationItem.rightBarButtonItem.customView setAlpha:0.0];

您甚至可以设置其显示/隐藏的动画

[UIView animateWithDuration:0.2 animations:^{
        [self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];

    }];

Show:

[self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];

Hide:

[self.navigationItem.rightBarButtonItem.customView setAlpha:0.0];

You can even animate its showing/hiding

[UIView animateWithDuration:0.2 animations:^{
        [self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];

    }];
黄昏下泛黄的笔记 2024-11-06 19:16:16

将引用设置为 nil:

current_controller_in_navcontroller.navigationItem.rightBarButtonItem =  nil;

还要确保在 navController 当前显示的控制器中调用此函数,而不是在 navController 本身中调用此函数。

Set reference to nil:

current_controller_in_navcontroller.navigationItem.rightBarButtonItem =  nil;

Also be sure to call this in the controller currently shown by the navController, not for the navController itself.

貪欢 2024-11-06 19:16:16

如果右侧只有一个栏按钮项,则可以使用此一项,

self.navigationItem.rightBarButtonItem = nil;

假设您的右侧有多个栏按钮,例如,假设您的导航项的右侧有两个栏按钮项目(搜索按钮和过滤按钮)。现在右栏按钮项目是

self.navigationItem.rightBarButtonItems = [searchItem,filterItem]

并且您必须仅隐藏搜索按钮,您可以使用类似,

self.navigationItem.rightBarButtonItems = [filterItem]

现在发生的事情是,您可以从导航项中完全隐藏搜索按钮,并且过滤项出现在搜索项的位置

If you have only one bar button item in the right side you can use this one,

self.navigationItem.rightBarButtonItem = nil;

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

self.navigationItem.rightBarButtonItems = [searchItem,filterItem]

and you have to hide only search button, you can use like,

self.navigationItem.rightBarButtonItems = [filterItem]

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

请叫√我孤独 2024-11-06 19:16:16

这是马特针对故事板和故事板更新的解决方案。弧。请记住,IBOutlet 默认情况下是 __weak,因此您需要将其更改为强,以免过早发布。

@interface MAGTableViewController () <UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UIBarButtonItem *rightBarButton;

@end

@implementation MAGTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.navigationItem setRightBarButtonItem:nil];
}

- (IBAction)rightBarButtonItemTapped:(id)sender
{
    [self.view endEditing:YES];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self.navigationItem setRightBarButtonItem:self.rightBarButton];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self.navigationItem setRightBarButtonItem:nil];
}

@end

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.

@interface MAGTableViewController () <UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UIBarButtonItem *rightBarButton;

@end

@implementation MAGTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.navigationItem setRightBarButtonItem:nil];
}

- (IBAction)rightBarButtonItemTapped:(id)sender
{
    [self.view endEditing:YES];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self.navigationItem setRightBarButtonItem:self.rightBarButton];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self.navigationItem setRightBarButtonItem:nil];
}

@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文