UINavigationBar Touch

前端 未结 9 1279
北恋
北恋 2020-12-23 17:53

I would like to fire an event on touch when a user taps the title of the navigation bar of one of my views.

I\'m at a bit of a loss on whether I can access the view

9条回答
  •  萌比男神i
    2020-12-23 18:29

    None of the other answers worked well for me. Rather than add a gesture to an existing subview of the navigationBar, or replace the titleView, I simply added a clear UIView covering a good portion of the navigationBar...

    - (void) setupNavbarGestureRecognizer {
        // recognise taps on navigation bar to hide
        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showHideNavbar)];
        gestureRecognizer.numberOfTapsRequired = 1;
        // create a view which covers most of the tap bar to
        // manage the gestures - if we use the navigation bar
        // it interferes with the nav buttons
        CGRect frame = CGRectMake(self.view.frame.size.width/4, 0, self.view.frame.size.width/2, 44);
        UIView *navBarTapView = [[UIView alloc] initWithFrame:frame];
        [self.navigationController.navigationBar addSubview:navBarTapView];
        navBarTapView.backgroundColor = [UIColor clearColor];
        [navBarTapView setUserInteractionEnabled:YES];
        [navBarTapView addGestureRecognizer:gestureRecognizer];
    }
    

提交回复
热议问题