簡體   English   中英

限制導航欄按鈕項中的觸摸區域?

[英]Limiting the touch area in the navigation barbutton item?

在iPhone中,我知道導航欄按鈕的觸摸在導航欄下也有一些擴展。但是我需要將用戶交互限制為一個特定的限制。我能夠做到這一點。有人可以幫助我嗎?

實現輕擊手勢識別器,並將您的控制器設置為委托。 然后執行:

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

}

您可以自己實現自定義導航欄,並在其后方隱藏隱藏的導航欄“引擎”,並使自定義導航欄上的按鈕具有所需的自定義行為/視覺效果。 如果要實現手勢邏輯(平移)以切換導航頁面,這也可能很有用。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM