2012年6月30日 星期六

Change UIActionSheet Style

 首先要記得繼承UIActionSheetDelegate

修改這個UIActionSheet的預設函式
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    // 設定UIActionSheet的backround圖片
    UIImage *theImage = [UIImage imageNamed:@"selectbg.png"];    
    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彈出後的按鈕樣式
UIActionSheet* Action = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"加入" otherButtonTitles:nil, nil];
    
[[[Action valueForKey:@"_buttons"] objectAtIndex:0] setImage:[UIImage imageNamed:@"select_bt.png"] forState:UIControlStateNormal];
    
[[[busAction valueForKey:@"_buttons"] objectAtIndex:1] setImage:[UIImage imageNamed:@"select_bt2.png"] forState:UIControlStateNormal];
    
[Action showInView:self.view];

沒有留言:

張貼留言