actionSheet的subviews一直为空 为什么

  UIActionSheet *myActionSheet = [[UIActionSheet alloc]
                                    initWithTitle:nil
                                    delegate:self
                                    cancelButtonTitle:@"取消"
                                    destructiveButtonTitle:nil
                                    otherButtonTitles: @"分享",@"删除",@"举报",nil];
    [myActionSheet showInView:self.view];
 
}

-(void)willPresentActionSheet:(UIActionSheet *)actionSheet{
    NSLog(@"actionSheet.subviews:%@",actionSheet.subviews);
    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];
        }
    }
}

打印出来`actionSheet.subviews:(
)`
为什么为空啊 明明有3个按钮

阅读 3.6k
1 个回答

以前的UIAlertView是可以拿到subview的,但是后来拿不了了。
苹果这样做的目的是不想让开发者去定义这些View。
所以说,不是调用subviews方法就一定可以拿到里面的View的。

苹果要做到这一点,我猜测有两种方式:
1.根本就没有subview,哪些按钮使用的是Layer或者重绘。
2.重载这些类的subviews方法让它返回空数组。(这个可能性最大)

自定义的方式苹果已经限制了,这个可能比较折腾。
你可以试试这个库,支持自定义View:JGActionSheet

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题