Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActionSheet is not showing separator on the last item on iOS 7 GM

It could be probably a bug on iOS7. But the last button is not separated from the previous oneUIActionSheet is missing separator on the last button

As you can see from the image. This happens on both Simulator and device using iOS7 GM. Does everyone else has the same problem?

UIActionSheet *actionSheet = [[UIActionSheet alloc]
               initWithTitle:@"Title"
               delegate:self
               cancelButtonTitle:nil
               destructiveButtonTitle:nil
               otherButtonTitles:@"First", @"Second", @"Third", @"Fourth", nil];
[actionSheet showInView:self.view];

As you can see the code is quite simple. Any idea on how to fix the problem? Or some third party library I can use instead of UIActionSheet ?

like image 272
Punty Avatar asked Sep 13 '13 16:09

Punty


3 Answers

I think ActionSheet requires a cancel button.So you can add the cancel button title.

Another way is: Specify actionSheet's cancelButtonIndex.

For example,in your case, you can add a "Cancel" in otherButtonTitles at index 4 and then specifiy actionSheet.cancelButtonIndex = 4.

like image 103
Yiding Avatar answered Sep 21 '22 04:09

Yiding


I found a way to make it work on iPhone and iPad in the least hacky way:

  1. Only init the UIActionSheet with a title
  2. Add your buttons
  3. Add a "CANCEL" button at last
  4. set the CancelButtonIndex to that last index

I assume that the missing separator is caused by the cancel button not being recognized as a separate case when adding it first or through the init.

like image 31
teebot Avatar answered Sep 19 '22 04:09

teebot


I found that adding a cancel button with an empty string after initialization works. The cancel button won't show up and the separator shows up.

[sheet addButtonWithTitle: @""];
[sheet setCancelButtonIndex: sheet.numberOfButtons - 1];

But this only works for iPad. On iPhone, an empty cancel button shows up, but I found a hacky workaround to make it work. In addition to the above, in willPresentActionSheet add this code in:

NSInteger offset = 55;
CGRect superFrame = actionSheet.superview.frame;
superFrame.origin.y += offset;
[actionSheet.superview setFrame: superFrame];

// hide underlay that gets shifted with the superview
[(UIView*)[[actionSheet.superview subviews] objectAtIndex: 0] removeFromSuperview];

// create new underlay
CGRect underlayFrame = CGRectMake(0, -offset, superFrame.size.width, superFrame.size.height);
UIView* underlay = [[UIView alloc] initWithFrame: underlayFrame];
underlay.alpha = 0.0f;
[underlay setBackgroundColor: [UIColor colorWithWhite: 0.0f alpha: 0.4f]];
[actionSheet.superview insertSubview: underlay atIndex: 0];

// simulate fade in
[UIView animateWithDuration: 0.3f animations:^{
    underlay.alpha = 1.0f;
}];

This shifts down the sheet to hide the cancel button off the screen

like image 25
Chris Avatar answered Sep 19 '22 04:09

Chris