我尝试在点击按钮内更改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
的背景颜色?
答案 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?