简体   繁体   中英

tap gesture not added on navigation bar

bigLabel = [[UILabel alloc] init];
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
[tap setNumberOfTapsRequired:1];
[bigLabel addGestureRecognizer:tap];
bigLabel.backgroundColor=[UIColor clearColor];
bigLabel.text = _referenceObject.textForCell;
//bigLabel.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"Header1.png"]];
bigLabel.font = [UIFont fontWithName:@"HelveticaNeueLTStd-Bd" size: 21.0];
bigLabel.font =[UIFont boldSystemFontOfSize:21.0f];

bigLabel.textColor = [UIColor whiteColor];
[bigLabel sizeToFit];

[self.navigationItem setTitleView:bigLabel];

Use the following code :

UITapGestureRecognizer* tapRecon = [[UITapGestureRecognizer alloc]
              initWithTarget:self action:@selector(navigationBarTap:)];
    tapRecon.numberOfTapsRequired = 1;
    [navController.navigationBar addGestureRecognizer:tapRecon];

you need to set

bigLabel.userInteractionEnabled = YES;

because by default UILabel instances userInteractionEnabled is NO

Try setting a frame to it like so:

UIView *iv = [[UIView alloc] initWithFrame:CGRectMake(0,0,32,32)];
[iv setBackgroundColor:[UIColor whiteColor]];
self.navigationItem.titleView = iv;
 UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc]
                                            initWithTarget:self
                                            action:@selector(hideKeyBoard)];

    tapGesture.cancelsTouchesInView = NO;
    [self.navigationController.view addGestureRecognizer:tapGesture];


-(void)hideKeyBoard
{
    [self.view endEditing:YES];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM