2015-10-11 168 views
0

我嘗試過改變UIActionSheet的背景顏色像一個按鈕,點擊裏面以下,更改UIActionSheet iOS的背景顏色?

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

創建和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]; 
} 

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

+0

我做過'yourActionSheet.delegate = self; yourActionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;'在按鈕點擊。可以嗎?但我仍然有這個問題。 – codebot

+0

沒有人,你在那個地方創建UIActionsheet你需要添加行,等待 –

+0

我在按鈕點擊方法內創建操作表 – codebot