0

I tried to label a UIbutton which used to work until I added a UIlabel layer.

At first the button looked like this and later I filled the space either side of the label thus

thisenter image description here

When I click either button the following error report is logged:

unexpected nil window in _UIApplicationHandleEventFromQueueEvent, _windowServerHitTestWindow: <UIWindow: 0x7fe460e338d0; frame = (0 0; 320 480); opaque = NO; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7fe460e326a0>; layer = <UIWindowLayer: 0x7fe460e33d80>> 

The button works if I click above the 4 letters or the ^ symbols.

Adding the label layer seems to effectively mask the UIButton. The hot spot (i.e. where the button works) seems to be above the letters or the ^ symbols. Can anyone explain why this might be happening ?

GlobalView.h file imports Quartz.Core/QuartzCore.h and UIKit/UIKit.h

Here’s the code in GlobalView.m

[UIButton buttonWithType:UIButtonTypeSystem];
UIButton *helpButton    = [UIButton buttonWithType:UIButtonTypeRoundedRect];
helpButton.frame = CGRectMake(72.0f, 472.0f, 188.0f, 32.0f);
helpButton.layer.cornerRadius = 10;
[helpButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[[helpButton layer] setBorderWidth:0.5f];
[helpButton addTarget:self action:@selector(goToHelp) forControlEvents:UIControlEventTouchUpInside];

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(72.0f, 472.0f, 188.0f, 32.0f);
label.text=@“help”;
[label setTextAlignment: NSTextAlignmentCenter];
label.textColor=[UIColor whiteColor];
[label.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[helpButton addSubview:label];
[self.view addSubview:helpButton];

Is the error log hinting that there is something I can do to recognise a tap gesture ? or is it better to try and get the UIButton to respond ?

CHANGE #1

The same problem occurs when I use setTitle to label the button

    [UIButton buttonWithType:UIButtonTypeSystem];
UIButton *helpButton    = [UIButton buttonWithType:UIButtonTypeRoundedRect];
helpButton.frame = CGRectMake(72.0f, 472.0f, 188.0f, 32.0f);
[helpButton setTitle: @"Transposed Dekany" forState: UIControlStateNormal];
helpButton.layer.cornerRadius = 10;
[helpButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[[helpButton layer] setBorderWidth:0.5f];
[helpButton addTarget:self action:@selector(goToHelp) forControlEvents:UIControlEventTouchUpInside];

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0,0,188,32)];
[label.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[helpButton addSubview:label];
[self.view addSubview:helpButton];

CHANGE #2

    UIButton *helpButton           = [UIButton buttonWithType:UIButtonTypeCustom];
[helpButton setTitleColor:[UIColor whiteColor] forState: UIControlStateNormal];
[helpButton setTitle: @"^^^^^^^Help^^^^^^^^^" forState: UIControlStateNormal];
helpButton.frame               = CGRectMake(72.0f, 472.0f, 188.0f, 32.0f); //same width and height
helpButton.layer.cornerRadius  = 10;
[helpButton.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[[helpButton layer] setBorderWidth:0.5f];
helpButton.clipsToBounds       = YES;
[helpButton addTarget:self action:@selector(goToHelp) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:helpButton];
Greg
  • 1,750
  • 2
  • 29
  • 56
  • You mean, you getting crash, when u touch outside the button? – Shebin Koshy Feb 16 '16 at 07:09
  • @Shebin Koshy: not quite. When I touch in an area of the button that is not covered by text it works and when I touch in an area that is covered by text it crashes. And I just checked, if I touch outside the area of the button, there is no error reported, which I'd expect. – Greg Feb 16 '16 at 10:01

0 Answers0