문제

I have a UIActionSheet that I'm conditionally adding buttons to. If there are a lot of buttons (more so for portrait view), the destructive button detaches from the action sheet (as expected) to allow the items to scroll. The thing that is not right, is it does not have a background to it.

iOS7 screenshot

UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil 
                                                  delegate:self
                                         cancelButtonTitle: nil
                                    destructiveButtonTitle:@"Changed my mind"
                                         otherButtonTitles:MAILBUTTON, BOOKMARKSBUTTON, CLEARCOOKIESBUTTON, nil];


if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    [sheet addButtonWithTitle:CAMERABUTTON];
}

sheet.tag = ACTION_SHEET;

if ([UIPrintInteractionController isPrintingAvailable])
{
    [sheet addButtonWithTitle:PRINTBUTTON];
}   

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([prefs boolForKey:BUMP_ENABLED_KEY] == YES)
{
    [sheet addButtonWithTitle:BUMPRECEIVEBUTTON];
    if ([prefs boolForKey:BUMP_ALLOW_SENDING_KEY] == YES)
        [sheet addButtonWithTitle:BUMPSENDBUTTON];
}

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

[sheet showInView:self.view];

It looks fine in portrait mode, the destructive button has a background. If I add a lot more buttons though, it has the same behavior seen above.

도움이 되었습니까?

해결책

Found a solution, it's a bit hacky. First I check if it's landscaped and that there are more than 5 buttons. When there are 6 or more, that's when it changes to a scrolling action sheet and that's when the problem occurs. If all that is true, I set the title to an empty string, with a space

[sheet setTitle: @" "];

This gives the white background to the button, but it leaves us with a big empty white space above, given you don't want a title.

To fix this, in willPresentActionSheet, I slide down the backdrop

        if (actionSheet.title)
        {
            if ([actionSheet.title.trim isEqualToString: @""])
            {
                CGRect backdropFrame = [[[actionSheet subviews] objectAtIndex: 0] frame];
                backdropFrame.origin.y += 45;
                backdropFrame.size.height -= 55;
                [[[actionSheet subviews] objectAtIndex: 0] setFrame: backdropFrame];
            }
        }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top