How do I show/hide a UIBarButtonItem?

前端 未结 30 2278
执念已碎
执念已碎 2020-11-28 01:18

I created a toolbar in IB with several buttons. I would like to be able to hide/show one of the buttons depending on the state of the data in the main window.

30条回答
  •  再見小時候
    2020-11-28 02:08

    Complementing Eli Burke`s response, if your UIBarButtonItemhas a background image instead of a title, you can use the code:

    -(void)toggleLogoutButton:(bool)show{
        if (show) {
            self.tabButton.style = UIBarButtonItemStyleBordered;
            self.tabButton.enabled = true;
            UIImage* imageMap = [UIImage imageNamed:@"btn_img.png"];
            [((UIButton *)[self.tabButton customView]) setBackgroundImage:imageMap forState:UIControlStateNormal];
        } else {
            self.tabButton.style = UIBarButtonItemStylePlain;
            self.tabButton.enabled = false;
            [((UIButton *)[self.tabButton customView]) setBackgroundImage:nil forState:UIControlStateNormal];
        }
    }
    

提交回复
热议问题