Home > 移动互联, 语言编程 > iOS开发之自定义系统的UIActionSheet修改按钮的属性

iOS开发之自定义系统的UIActionSheet修改按钮的属性

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 {
            }
        }
    } 
}
Categories: 移动互联, 语言编程 Tags:
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.