How do I show/hide a UIBarButtonItem?

前端 未结 30 2279
执念已碎
执念已碎 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 01:50

    I know this answer is late for this question. However, it might help if anybody else faces a similar situation.

    In iOS 7, to hide a bar button item, we can use the following two techniques :-

    • use SetTitleTextAttributes :- This works great on bar button items like "Done", "Save" etc. However, it does not work on items like Add, Trash symbol etc.(atleast not for me) since they are not texts.
    • use TintColor :- If I have a bar button item called "deleteButton" :-

    To hide the button, I used the following code:-

    [self.deleteButton setEnabled:NO]; 
    [self.deleteButton setTintColor: [UIColor clearColor]];
    

    To show the button again I used the following code:-

    [self.deleteButton setEnabled:YES];
    [self.deleteButton setTintColor:nil];
    

提交回复
热议问题