自定義UIActionSheet背景、按鈕


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寫的,如果使用上有問題,可以修改一下源碼。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM