简体   繁体   中英

Limiting the touch area in the navigation barbutton item?

在iPhone中,我知道导航栏按钮的触摸在导航栏下也有一些扩展。但是我需要将用户交互限制为一个特定的限制。我能够做到这一点。有人可以帮助我吗?

Implement a tap gesture recognizer and set your controller as delegate. Then implement:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

   // [touch locationInView] -> gives the point where the user touched
   // If the touch point belongs to your frame then return YES
   // else return NO

}

You can implement a custom navigation bar yourself with hidden navigation bar "engine" behind and have the buttons on the custom navbar with the required custom behavior/visuals. This also can be useful if you want to implement gesture logic (pan) to switch navigation pages.

AppDelegate.h:

@property (strong, nonatomic) UINavigationController *navigationController;

AppDelegate.m:

YourMainViewController *yourmainViewController = [[YourMainViewController alloc] init];
_navigationController = [[UINavigationController alloc] yourmainViewController];
[_navigationController setNavigationBarHidden:TRUE];
[self.window setRootViewController:_navigationController];
[self.window makeKeyAndVisible];

YourMainViewController.m: implement your view with custom navigation image and added buttons for navigation either using Interface Builder or programatically. For example programatically create your view:

- (void)loadView {  
...
    UIImageView *tmp_mynavbar = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CustomNavBG.png"]];
    tmp_mynavbar.frame = CGRectMake(0, 0, 320, 44);
    [self.view addSubview:tmp_mynavbar];
    UIButton *tmp_addbutton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    tmp_addbutton.frame = CGRectMake(260, 10, 40, 20);
    [tmp_addbutton setTitle:@"Add" forState:UIControlStateNormal];
    [tmp_addbutton setBackgroundImage:[UIImage imageNamed:@"CustomNavAddBtn.png"] forState:UIControlStateNormal];
    [tmp_addbutton addTarget:self action:@selector(pressedbuttonAddItem:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tmp_addbutton];

// create button with the required size, user interaction area (add image then add transparent button with different size, etc)
// also add a back button
...
}

then implement custom navigation button behavior (the add/next button and also the back button)

-(void) pressedbuttonAddItem:(id) sender {
    AppDelegate *app = (AppDelegate*) [[UIApplication sharedApplication] delegate];
    DetailViewController *detailViewController = [[DetailViewController alloc] init];
    [[app navigationController] detailViewController animated:YES];
}

-(void) pressedbuttonBack:(id) sender {
    AppDelegate *app = (AppDelegate*) [[UIApplication sharedApplication] delegate];
    [[app navigationController] popViewControllerAnimated: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