UIToolbar items disappear - Weird bug in ios6

2019-07-06 11:46发布

问题:

This is the current setup.

I have the navigationController's toolbar with 5 buttons, and tapping on them hides the toolbar for 2 seconds, and then shows the toolbar again (except the 5th button - which brings up an actionsheet with buttons (ACTION & CANCEL)).

On tapping on the 1-4 buttons, I do a self.navigationController.toolbarHidden = YES; and after exactly 2 seconds, I set the self.navigationController.toolbarHidden = NO; and this brings back the toolbar, and everything's fine.

On tapping the 5th button, which brings up action sheet.

  1. If i tap on CANCEL actionsheet => actionSheet dismissed => Toolbar is fine.
  2. If I tap on ACTION button I do a self.navigationController.toolbarHidden = YES; and after 2 seconds... self.navigationController.toolbarHidden = NO; but now... The toolbar buttons are GONE.

Further investigating...

I can see the the toolbarButtons seem to have their alpha values set to 0.

I have no idea why the toolbar items' alpha are set to value = 0 after actionsheet operation.

Can anyone tell me the root cause for this?

回答1:

Have you tried setting the toolbar items array to nil? I had this same problem and it turned out that putting a check around when you set the toolbar's items seemed to work:

if ([self.navigationController.toolbar.items count] > 0) {
   [self.navigationController.toolbar setItems:nil];
}

[self.navigationController.toolbar setItems:toolbarItems]; //toolbarItems is your array of UIBarButtonItems.


回答2:

I managed to fix the issue in a different way. I hide the toolbar when the action sheet comes up, and after the buttonAction(), I essentially show the toolbar again.

This solves the problem where the toolbarItems disappear.

But the reason as to why the toolbarItems disappear and set alpha=0 is still a mystery for me. If anyone finds out the reason, please let me know :)



回答3:

I had the same issue and reproduced it in one of the samples. It appears to be a bug in iOS6 when setting up toolbar items manually in loadView / viewDidLoad, then later calling an ActionSheet.

The code below is a workaround it -

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSArray* items = self.toolbarItems;
    [self setToolbarItems:nil];
    [self setToolbarItems:items animated:NO];
}


回答4:

I solve it by moving action code to separate method and then calling it through sending message performSelector:withObject:afterDelay: with 0.25f second delay

Example:

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [self performSelector:@selector(logout) withObject:nil afterDelay:0.25f];
    }
}


回答5:

I don't know if it's the case, I found out that the disappeared items were actually in the toolbar, but placed over the bottom of the view. Maybe resetting them on certain circumstances may cause autolayout issues.

I fixed it by calling the setNeedLayout method on the viewcontroller's view (not the navigationControllers')

self.toolbarItems = toolButtons;
[self.view setNeedsLayout];