Hiding UINavigationItem's bar button

后端 未结 11 986
粉色の甜心
粉色の甜心 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:18
    func showOrHideButton() {
            isEnabled ? showButton() : hideButton()
        }
    
     func showButton() {
            navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .compose, target: self, action: #selector(action))
        }
    
        func hideButton() {
            navigationItem.leftBarButtonItem = nil
        }
    
    0 讨论(0)
  • 2021-01-04 07:21

    I have a easy function to make this. I have a navigation like this. It comes form Interface Builder, it has a background image.

    @IBOutlet weak var memberBtn: UIBarButtonItem!
    

    you can hide/show it by:

    func hideMemberBtn() {
        memberBtn.isEnabled = false
        memberBtn.tintColor = UIColor.clear
    }
    func showMemberBtn() {
        memberBtn.isEnabled = true
        memberBtn.tintColor = UIColor.white
    }
    

    It's easy but it work for me. You can change tintColor as you needed. Hope for help :]

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

    There's nothing in the documentation to suggest bar items have a hidden property.

    Why not set

    self.navigationItem.leftBarButtonItem = nil; 
    

    when the user isn't editing, then set

    self.navigationItem.leftBarButtonItem = whateverBarButtonItem; 
    

    when the user is editing? This requires either re-creating the button each time or storing it for the duration of the view's lifecycle. Neither is terribly painful, but no, not nearly as easy as a .hidden property.

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

    This solution work for me

    UIView *myView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 300, 30)];
    UIBarButtonItem *btnL = [[UIBarButtonItem alloc]initWithCustomView:myView];
    self.navigationItem.leftBarButtonItem = btnL;
    
    0 讨论(0)
  • 2021-01-04 07:29

    To hide/disable

    [self.navigationItem.leftBarButtonItem setEnabled:FALSE];
    

    To show/enable

    [self.navigationItem.leftBarButtonItem setEnabled:TRUE];
    
    0 讨论(0)
提交回复
热议问题