How do I show/hide a UIBarButtonItem?

前端 未结 30 2362
执念已碎
执念已碎 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:07

    It is possible to hide a button in place without changing its width or removing it from the bar. If you set the style to plain, remove the title, and disable the button, it will disappear. To restore it, just reverse your changes.

    -(void)toggleBarButton:(bool)show
    {
        if (show) {
            btn.style = UIBarButtonItemStyleBordered;
            btn.enabled = true;
            btn.title = @"MyTitle";
        } else {
            btn.style = UIBarButtonItemStylePlain;
            btn.enabled = false;
            btn.title = nil;
        }
    }
    

提交回复
热议问题