UIButton 点击事件被遮挡 按钮无法点击

iOS 码拜 9年前 (2015-10-23) 3216次浏览
代码如下

 UIView *footView =[[UIView alloc]initWithFrame:CGRectMake(0,rect.size.height-64-40, rect.size.width,40)];
        [self.view addSubview:footView];
 goShopcartBtn = [[UIButton alloc]initWithFrame:CGRectMake(235, 3, 77, 33)];
        [goShopcartBtn addTarget:self action:@selector(gotoShopcart:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 77, 30)];
        titleLabel.text = @"去购物车";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor whiteColor];
        [goShopcartBtn addSubview:titleLabel];
        goShopcartBtn.layer.cornerRadius = 5.0;
        goShopcartBtn.backgroundColor = ORANGE_COLOR;
 [footView addSubview:goShopcartBtn];

页面上都显示出来了  但是按钮无法点击。

本人把footview的y轴 上调49 整个按钮就能全部点击。
本人的猜想是 本人 是有tabbar 进入这个页面的,虽然将tabbar隐藏了,但是控件还是在那里, 是不是本人自定义的view被tabbar遮挡了。

解决方案:5分
1.UIButton本身就可以设置文本啊,为什么要本人重新定义一个UILabel呢?

2.UIButton的点击没有反应应该是你添加的UILabel把它遮挡住了,原因是两个对象的大小基本一样,所以你点击的是UILabel,而不是UIButton,UILabel这个本身是没有点击事件的

解决方案:5分

view生命周期函数里写

解决方案:5分
购物车 那几个字 为什么不设置在button 里面呢? 你这样会被label 挡住, 要么你把 label 放到底层去 或让button 置顶
解决方案:5分
UILabel 是不会应影响到事件响应的,UILabel的重新改写了userInteractionEnabled 属性值为NO,并不会阻挡下方按钮的响应。

另外,隐藏的视图也是不会响应事件的。


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明UIButton 点击事件被遮挡 按钮无法点击
喜欢 (0)
[1034331897@qq.com]
分享 (0)