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

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

4

3 回答 3

1

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

在此处输入图像描述

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

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

要解决此问题,只需对齐您的视图,使它们彼此位于内部。

于 2012-11-27T03:31:39.643 回答
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变量名。

于 2012-11-27T03:31:43.327 回答
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];
于 2012-11-27T03:45:31.950 回答