How do I show/hide a UIBarButtonItem?

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

    One way to do it is use the initWithCustomView:(UIView *) property of when allocating the UIBarButtonItem. Subclass for UIView will have hide/unhide property.

    For example:

    1. Have a UIButton which you want to hide/unhide.

    2. Make the UIButtonas the custom view. Like :

    UIButton*myButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];//your button
    
    UIBarButtonItem*yourBarButton=[[UIBarButtonItem alloc] initWithCustomView:myButton];
    

    3. You can hide/unhide the myButton you've created. [myButton setHidden:YES];

提交回复
热议问题