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

时间:2013-03-14 07:37:43

标签: iphone ios ipad

在iphone中,我知道导航栏按钮在导航栏下也有一些扩展名。但是我需要将用户交互限制到一定限度。我能够做到这一点。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

实施点击手势识别器并将控制器设置为委托。然后执行:

- (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

}

答案 1 :(得分:0)

您可以使用隐藏的导航栏“引擎”自行实现自定义导航栏,并在自定义导航栏上显示具有所需自定义行为/视觉效果的按钮。 如果您想要实现手势逻辑(平移)来切换导航页面,这也很有用。

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:使用自定义导航图像实现您的视图,并使用Interface Builder或以编程方式添加导航按钮。例如,以编程方式创建视图:

- (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
...
}

然后实现自定义导航按钮行为(添加/下一个按钮以及后退按钮)

-(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];
}