2012-11-27 84 views
0

我有一个标签在上面的按钮。标签阻止按钮,所以我不能点击它。标签封锁按钮点击

timerView = [[UIView alloc] initWithFrame:CGRectMake(0, 278, 105, 27)]; 
[self.view addSubview:timerView]; 

UIButton *timerBackground = [UIButton buttonWithType:UIButtonTypeCustom]; 
[timerBackground setBackgroundImage:[UIImage imageNamed:@"Time Background.png"] forState:UIControlStateNormal]; 
[timerBackground setBackgroundImage:[UIImage imageNamed:@"Time Background.png"] forState:UIControlStateHighlighted]; 
[timerBackground addTarget:self action:@selector(openTimeChanger) forControlEvents:UIControlEventTouchUpInside]; 
timerBackground.frame = CGRectMake(-2 , 15, 102, 27); 
[timerView addSubview:timerBackground]; 

DateDay = [[UILabel alloc] initWithFrame:CGRectMake(6, 4, 20, 21)]; 
DateDay.backgroundColor = [UIColor clearColor]; 
DateDay.textColor = [UIColor whiteColor]; 
[timerBackground addSubview:DateDay]; 

我希望标签是'透明'的点击,所以我可以点击按钮。

回答

1

UILabel不会阻止触摸,但您的视图层次。我已经添加了一些颜色调试此:

enter image description here

  1. timerView - 绿色
  2. timerBackground - 红
  3. DateDay - 黑

正如你看到的,你的按钮是延长它的父项,所以绿色范围外的所有内容都是非活动的,实际上,如果你点击黑色矩形的顶部,按钮和父项在标签下重叠 - 你将得到轻击事件。

要解决这个问题,只需简单地对齐视图,使它们彼此相对。

0

试试这个,

DateDay.userInteractionEnabled = NO; 

还要检查是否有在此之上没有其他看法。并正确对齐您的视图以检测触摸。如果您将框架设置为CGRectMake(-2 , 15, 102, 27);,则timerView之外的区域不会检测到任何触摸。

检查,如果你可以如下修改代码来检测触摸,

timerView = [[UIView alloc] initWithFrame:CGRectMake(0, 278, 105, 27)]; 
[self.view addSubview:timerView]; 

UIButton *timerBackground = [UIButton buttonWithType:UIButtonTypeCustom]; 
[timerBackground setBackgroundImage:[UIImage imageNamed:@"Time Background.png"] forState:UIControlStateNormal]; 
[timerBackground setBackgroundImage:[UIImage imageNamed:@"Time Background.png"] forState:UIControlStateHighlighted]; 
[timerBackground addTarget:self action:@selector(openTimeChanger) forControlEvents:UIControlEventTouchUpInside]; 
timerBackground.frame = CGRectMake(0, 0, 100, 27); 
[timerView addSubview:timerBackground]; 

DateDay = [[UILabel alloc] initWithFrame:CGRectMake(6, 4, 20, 21)]; 
DateDay.backgroundColor = [UIColor clearColor]; 
DateDay.textColor = [UIColor whiteColor]; 
[timerBackground addSubview:DateDay]; 

如果你仍然想保持你的timerBackground的帧CGRectMake(-2 , 15, 102, 27);,将其添加为self.view一个子视图,而不是增加对timerView并设置帧,

timerBackground.frame = CGRectMake(0, 263, 100, 27);//or timerBackground.frame = CGRectMake(-2, 263, 102, 27); 

,并添加子视图作为,

[self.view addSubview:timerBackground]; 

在旁注中,请使用dateDay作为变量名称。

0

您的标签视图按钮上面只是改变这样

timerView = [[UIView alloc] initWithFrame:CGRectMake(0, 278, 105, 27)]; 
[self.view addSubview:timerView]; 

    DateDay = [[UILabel alloc] initWithFrame:CGRectMake(6, 4, 20, 21)]; 
    DateDay.backgroundColor = [UIColor clearColor]; 
    DateDay.textColor = [UIColor whiteColor]; 
    [timerBackground addSubview:DateDay]; 

    UIButton *timerBackground = [UIButton buttonWithType:UIButtonTypeCustom]; 
[timerBackground setBackgroundImage:[UIImage imageNamed:@"Time Background.png"]forState:UIControlStateNormal]; 
[timerBackground setBackgroundImage:[UIImage imageNamed:@"Time Background.png"]forState:UIControlStateHighlighted]; 
[timerBackground addTarget:self action:@selector(openTimeChanger) forControlEvents:UIControlEventTouchUpInside]; 
    timerBackground.frame = CGRectMake(-2 , 15, 102, 27); 
    [timerView addSubview:timerBackground]; 
代码