Hiding UINavigationItem's bar button

后端 未结 11 985
粉色の甜心
粉色の甜心 2021-01-04 07:04

I have added a BarButton item to the left of the nav.bar through Interface Builder and in the code I want this only to show in my table view\'s edi

相关标签:
11条回答
  • 2021-01-04 07:05

    I'm pretty sure the only way to "hide" it is to nil it out.

    self.navigationItem.leftBarButtonItem = nil;
    

    Though it's not a perfect answer to your question, since that basically gets rid of your button instead of hiding it. You'll either have to recreate it or keep your original button around and simply set the leftBarButtonItem back to your UIBarButtonItem.

    0 讨论(0)
  • 2021-01-04 07:06

    This works I tried it myself

    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.hidesBackButton = YES;      
    
    0 讨论(0)
  • 2021-01-04 07:07

    I just created my own "hide" function show below:

    - (void)hideClearButton:(BOOL)hide {
    
        if (hide) {
            self.navigationItem.leftBarButtonItem = nil;
        }
        else {
            self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
                                                     initWithTitle:NSLocalizedString(@"Clear", @"Recents")
                                                     style:UIBarButtonItemStylePlain
                                                     target:self action:@selector(clearAll:)];
    
        }
    }
    

    You can just call it like:

    [self hideClearButton:YES]; //hide it
    

    or

    [self hideClearButton:NO];  //show it
    
    0 讨论(0)
  • 2021-01-04 07:13

    Well making it nil was not a option because i wanted to show it again and didnt want to create a button again.

    so what i did was

     UIBarButtonItem *barButton =  (UIBarButtonItem *)self.navBar.topItem.leftBarButtonItem;
     barButton.customView.hidden = true;//Hide
     barButton.customView.hidden = false;//Show
    

    Works for me. (my leftBarButtonItem was created using customView)

    Hope it helps.

    0 讨论(0)
  • 2021-01-04 07:16

    You can use

    // Hide
    self.navigationItem.leftBarButtonItem = nil;
    
    // Show
    self.navigationItem.leftBarButtonItem = self.myBarButtonItem
    

    The key is making sure that you have a strong reference to the button item before nilling leftBarButtonItem.

    @property (strong, nonatomic) IBOutlet UIBarButtonItem *myBarButtonItem;
    
    0 讨论(0)
  • 2021-01-04 07:17

    You can use

    [self.navigationItem.leftBarButtonItem setEnabled:TRUE];
    

    as there is no other way to hide it. so just disable it.

    0 讨论(0)
提交回复
热议问题