PappaSmalls
PappaSmalls

Reputation: 349

Label blocking a button click

I have a button with labels over it. The labels are blocking the button so I cannot click it.

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

I want the label to be 'transparent' to clicks, so I can click the button.

Upvotes: 0

Views: 123

Answers (3)

iDev
iDev

Reputation: 23278

try this,

DateDay.userInteractionEnabled = NO;

Also check if there are no other views on top of this. And align your views properly to detect the touches. If you are setting your frame as CGRectMake(-2 , 15, 102, 27); the area which is outside the timerView wont detect any touches.

Check if you can change your code as below to detect the touches,

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

If you still want to keep your timerBackground's frame as CGRectMake(-2 , 15, 102, 27);, add it as a subview of self.view rather than adding on timerView and set your frame as,

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

and add subview as,

[self.view addSubview:timerBackground];

On a side note please use dateDay for variable names.

Upvotes: 0

Rose
Rose

Reputation: 437

Your Label view is top on the button just change the code like this

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

Upvotes: 0

Dmitry Shevchenko
Dmitry Shevchenko

Reputation: 32404

UILabel does not block touches, but your view hierarchy does. I've added some colors to debug this:

enter image description here

  1. timerView — green
  2. timerBackground — red
  3. DateDay - black

As you see, your button is extending its parent, so everything outside green bounds is inactive, actually, if you click in the very top of black rectangle, where button and parent overlap under the label—you will get a tap event.

To fix this simply align your views so that they're inside each other.

Upvotes: 1

Related Questions