2013-03-14 94 views
1

在iPhone中我已經知道navigationbarbutton觸摸也有一些擴展在導航欄下,但我需要限制用戶交互只有一定的限制。我可以做到這一點。任何人都可以幫助我嗎?限制導航欄按鈕項中的觸摸區域?

+0

一個快速的想法:使用可以把透明'UIView'所以你不能碰那部分! – Maulik 2013-03-14 07:40:17

+0

@maulik我有一個按鈕只是在嘮叨bar.so當我觸摸該按鈕有時導航欄按鈕被調用... – hacker 2013-03-14 07:42:50

+0

然後,我想你必須改變按鈕位置 – Maulik 2013-03-14 07:43:40

回答

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 

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