我在label 上加了一个 button 想看看userInteractionEnabled的效果 为什么button在label的下面?
  求大神简单讲一下用户交互的使用,以及在项目中的情况!

解决方案 »

  1.   

    1、userInteractionEnabled属性决定UIView是否接受并响应用户的交互,为啥要将UIButton添加到UILable上看效果
    2、关于Button在Label的下面,你可以试试 bringSubviewToFront添加视图控件,而不是subView
      

  2.   

    不好使啊  button 还是在label 下面  UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
        [label setBackgroundColor:[UIColor redColor]];
        [self.view addSubview:label];
        
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(20,20, 64, 64)];
        [button setBackgroundColor:[UIColor greenColor]];
        [label addSubview:button];
        [label bringSubviewToFront:button];
      

  3.   

    CGRectMake(0, 0, 100, 100)]  CGRectMake(0,0, 64, 64)  其实btn已经在lable的上面了。而且 [label bringSubviewToFront:button]; 是去掉。。只是你设置的坐标显示的问题
      

  4.   

    只要设置了label.text的属性就可以了,设置成空也成,但是一定要设置
      

  5.   

    我遇到和楼主一样的问题,设置label.text也不行,哪位大牛来解答下?
      

  6.   

    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 450, 350, 40)];
        label.textAlignment=2;
        [self.view addSubview:label];
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame=CGRectMake(0, 0, 40, 28);
        [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    //     [btn setBackgroundColor:[UIColor grayColor]];
        [btn setTitle:@"点点" forState:UIControlStateNormal];
        [label addSubview:btn];
    //    [label bringSubviewToFront:btn];
        label.userInteractionEnabled=YES;
      

  7.   

    button的父视图需要设置user interaction属性为yes
      

  8.   

    //    self.textBackLabel.clipsToBounds = YES; //显示子视图
    //    self.textBackLabel.text = @"中文";  //设置文字可以显示子视图 必须在后面 中文不显示 数字英文可以显示
    //   self.textBackLabel.backgroundColor = [UIColor grayColor]; //设置了颜色不显示子视图
    //    [self.textBackLabel layoutIfNeeded ]; //显示子视图
    百度 code_小马 ,我的简书里边有详细解释