更改UIActionSheet iOS的背景颜色?

时间:2015-10-11 10:57:13

标签: ios objective-c uiactionsheet

我尝试在点击按钮内更改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];

但我没有改变背景颜色。我从stackoverflow得到了这个。我还试图用委托方法

来做到这一点
-(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的背景颜色?

1 个答案:

答案 0 :(得分:1)

  

要在iOS 8及更高版本中创建和管理操作表,请使用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?