iOS中UIActionSheet在SDK中只提供了少數幾種樣式,這些樣式基本可以滿足開發需求了,但是也會遇到比較麻煩的要求,這是就需要單獨定製UIActionSheet,通過修改button的屬性來實現修改,如果是ios4或者之前版本操作起來比較複雜,但是ios5以後,UIActionSheet中的button換成UIButton類型以後就比較容易了,可以直接便利UIActionSheet的所有subview,找到對應的button就可以修改它的屬性,實現還是很簡單的,需要注意的是UIActionSheet中button列表中的button索引id可以直接使用tag屬性來直接擷取,這就很方便找到它們了
參考代碼;
代碼如下 |
複製代碼 |
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet { if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9) { for (UIView *view in actionSheet.subviews) { if (view.tag == 2) { UIButton *button = (UIButton *) view; //改變背景 [button setBackgroundImage:[button backgroundImageForState:UIControlStateHighlighted] forState:UIControlStateNormal]; //改變顏色 [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; //btn的選擇狀態,否則選擇後不變背景 [button setSelected:YES]; } else { } } } else { //IOS4之前版本下按鈕不是繼承於UIButton而是UIThreePartButton for (UIView *view in actionSheet.subviews) { if (view.tag == 2) { UIControl *btn = (UIControl *) view; [btn setSelected:YES]; } else { } } } } |