gbesler gbesler - 1 month ago 15
Objective-C Question

UITapGestureRecognizer does not respond on touch

I am creating a

UILabel
with attributed string and assign it to navigation bar title. It is displayed where I want. I also want to attach it
UITapGestureRecognizer
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
action:@selector(clickTitleLabel)];
[label addGestureRecognizer:tapGesture];

Answer

Step-1

initially check once setUserInteractionEnabled:YES or not

[label setUserInteractionEnabled:YES];

Step-2

else check once frame and clickable area 

step-3

add number of tap required

tapGesture.numberOfTapsRequired = 1;