gbesler gbesler - 10 months ago 74
Objective-C Question

UITapGestureRecognizer does not respond on touch

I am creating a

with attributed string and assign it to navigation bar title. It is displayed where I want. I also want to attach it
but I can't make the function called in the recognizer. Below you can see the code I am trying it.

label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.numberOfLines = 4;

// set different font for title and subtitle
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"\n Title \n \n"];
//[string addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:1.0] range:NSMakeRange(0,string.length)];

// set line spacing
NSMutableParagraphStyle *paragrahStyle = [[NSMutableParagraphStyle alloc] init];
[paragrahStyle setLineSpacing:-10];
[paragrahStyle setAlignment:NSTextAlignmentCenter];
[string addAttribute:NSParagraphStyleAttributeName value:paragrahStyle range:NSMakeRange(0, [string length])];

label.font = [UIFont fontWithName:@"Avenir-Medium" size:22.0f];
self.navigationItem.titleView = label;
label.attributedText = string;
[label sizeToFit];

UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self
[label addGestureRecognizer:tapGesture];

Answer Source


initially check once setUserInteractionEnabled:YES or not

[label setUserInteractionEnabled:YES];


else check once frame and clickable area 


add number of tap required

tapGesture.numberOfTapsRequired = 1;