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

后端 未结 8 445
暗喜
暗喜 2020-12-03 04:31

It could be probably a bug on iOS7. But the last button is not separated from the previous one\"UIActionSheet

相关标签:
8条回答
  • 2020-12-03 04:42

    It seems that the initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles: method is buggy and you shouldn't specify any cancel button here at all. Btw, it seems that a destructive button set in that method also won't work very well.

    Instead of it you should:

    • provide a custom cancel button as the last button in the buttons array, eg: ["Action 1", "Action 2", "Close"] or sheet.addButtonWithTitle("Close")
    • manually set the cancel button index, e.g. sheet.cancelButtonIndex = 2

    Then everything will work as expected. On the iPad the button will be automatically hidden and on the iPhone it will be styled and placed in the proper way.

    0 讨论(0)
  • 2020-12-03 04:52

    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

    0 讨论(0)
  • 2020-12-03 04:54
    UIActionSheet *asAccounts = [[UIActionSheet alloc]
                                initWithTitle:Localized(@"select_an_account")
                                delegate:self
                                cancelButtonTitle:nil
                                destructiveButtonTitle:nil
                                otherButtonTitles: nil];
    
    for (int i=0; i<[result count]; i++) {
        ACAccount *acct = [result objectAtIndex:i];
        [asAccounts addButtonWithTitle:[acct username]];
        asAccounts.tag = i;
    }
    
    [asAccounts addButtonWithTitle:Localized(@"Cancel")];                
    asAccounts.cancelButtonIndex = result.count;
    [asAccounts showInView:self.view];
    
    0 讨论(0)
  • 2020-12-03 05:00

    The simplest fix is to pass @"" to the cancel button title instead of nil during allocation.

    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                   initWithTitle:@"Title"
                   delegate:self
                   cancelButtonTitle:@"" // change is here
                   destructiveButtonTitle:nil
                   otherButtonTitles:@"First", @"Second", @"Third", @"Fourth", nil];
    [actionSheet showInView:self.view];
    
    0 讨论(0)
  • 2020-12-03 05:03

    If you have a cancel button, the last row will be shown. That is the temp fix I am using now. Do not know any solution if you do not want a cancel button to show

    0 讨论(0)
  • 2020-12-03 05:05
        - (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
        if ([UIDevice currentDevice].systemVersion.floatValue < 8.0f) {
            UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(8, 88, actionSheet.frame.size.width - 16, 0.5)];
            separator.backgroundColor = [UIColor colorWithRed:219.0f/255 green:219.0f/255 blue:223.0f/255 alpha:1];
            [actionSheet addSubview:separator];
        }
    }
    

    Every button has height 44. My actionSheet doesn't have title. And I wanted to add separator between second and third buttons. That's why I use number 88.

    0 讨论(0)
提交回复
热议问题