标签阻止按钮单击

时间:2012-11-27 02:34:05

标签: objective-c

我有一个带有标签的按钮。标签正在阻止按钮,因此我无法单击它。

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];

我希望标签对点击“透明”,因此我可以点击按钮。

3 个答案:

答案 0 :(得分:1)

UILabel不会阻止触摸,但您的视图层次结构会阻止触摸。我添加了一些颜色来调试它:

enter image description here

  1. timerView - 绿色
  2. timerBackground - red
  3. DateDay - black
  4. 如您所见,您的按钮正在扩展其父级,因此绿色边界之外的所有内容都处于非活动状态,实际上,如果您单击黑色矩形的顶部,按钮和父级在标签下重叠 - 您将获得一个点击事件

    要解决这个问题,只需将视图对齐,以便它们彼此在一起。

答案 1 :(得分: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作为变量名称。

答案 2 :(得分: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];