代码改变世界

自定义UIActionSheet背景、按钮

2012-10-14 19:43  Y_York  阅读(3269)  评论(1编辑  收藏  举报

1. 自定义ActionSheet背景

首先 #import <QuartzCore/QuartzCore.h>

然后实现UIActionSheetDelegate方法:

- (void)willPresentActionSheet:(UIActionSheet*)actionSheet {
    UIImage*theImage =[UIImage imageNamed:@"detail_menu_bg.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];}
}
 
2.自定义ActionSheet按钮背景图片

网上找了很多方法, stackOverFlow上有在actionSheet 的subviews里找到ValueforKey:@"_button"类似的代码,确实能实现更改背景图片,但这触碰到了apple的私有api,有可能进不了app store。同理,改UIThreePartButton的也不推荐。
最方便的方法是:http://code4app.com/ios/CMActionSheet/5024d0896803fa8056000000
这个第三方控件使用很方便,代码也很清晰。button的点击事件回调方法是block写的,如果使用上有问题,可以修改一下源码。