简体   繁体   English

限制导航栏按钮项中的触摸区域?

[英]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: AppDelegate.h:

@property (strong, nonatomic) UINavigationController *navigationController;

AppDelegate.m: 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. YourMainViewController.m:使用自定义导航图像和添加的用于使用Interface Builder或以编程方式进行导航的按钮来实现视图。 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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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