How do I show/hide a UIBarButtonItem?

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

    Some helper methods I thought I'd share based upon lnafziger's accepted answer as I have multiple toolbars and multiple buttons in each:

    -(void) hideToolbarItem:(UIBarButtonItem*) button inToolbar:(UIToolbar*) toolbar{
        NSMutableArray *toolbarButtons = [toolbar.items mutableCopy];
        [toolbarButtons removeObject:button];
        [toolbar setItems:toolbarButtons animated:NO];
    }
    
    -(void) showToolbarItem:(UIBarButtonItem*) button inToolbar:(UIToolbar*) toolbar atIndex:(int) index{
        NSMutableArray *toolbarButtons = [toolbar.items mutableCopy];
        if (![toolbarButtons containsObject:button]){
            [toolbarButtons insertObject:button atIndex:index];
            [self setToolbarItems:toolbarButtons animated:YES];
        }
    }
    

提交回复
热议问题