更改 UIActionSheet iOS 的背景颜色?

Change background color of UIActionSheet iOS?

我尝试更改 UIActionSheet 的背景颜色,例如在单击按钮后,

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Unfollow"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:@"Unfollow"
                                                    otherButtonTitles:nil];

    [actionSheet showInView:self.parent.view];
    CGSize mySize = actionSheet.bounds.size;
    CGRect myRect = CGRectMake(0, 0, mySize.width, mySize.height);
    UIImageView *redView = [[UIImageView alloc] initWithFrame:myRect];
    [redView setBackgroundColor:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.5]];
    [actionSheet insertSubview:redView atIndex:0];

但是我更改背景颜色失败了。我从 Whosebug 得到了这个。而且我还尝试使用委托方法来做到这一点,

-(void)willPresentActionSheet:(UIActionSheet *)actionSheet{
    UIImage *theImage = [UIImage imageNamed:@"crownImg"];
    theImage = [theImage stretchableImageWithLeftCapWidth:32 topCapHeight:32];
    CGSize theSize = actionSheet.frame.size;
    // draw the background image and replace layer content
    UIGraphicsBeginImageContext(theSize);
    [theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];
    theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [[actionSheet layer] setContents:(id)theImage.CGImage];
}

这也没有给出预期的输出。有没有办法改变 UIActionSheet 的背景颜色?

To create and manage action sheets in iOS 8 and later, use UIAlertController

试试这个

  #import "QuartzCore/QuartzCore.h"

在您的视图控制器中添加 UIActionSheetDelegate

@interface ViewController : UIViewController <UIActionSheetDelegate> 


yourActionSheet.delegate = self;
yourActionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;


- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
[[actionSheet layer] setBackgroundColor:[UIColor redColor].CGColor];
}

需要更多参考:iPhone development: How to create colored or translucent background for UIActionSheet?