2015-04-01 11 views

回答

2

嘗試以下操作:

1)添加按鈕,導航

UIButton *titleLabelButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[titleLabelButton setTitle:@"myTitle" forState:UIControlStateNormal]; 
titleLabelButton.frame = CGRectMake(0, 0, 70, 44); 
titleLabelButton.font = [UIFont boldSystemFontOfSize:16]; 
[titleLabelButton addTarget:self action:@selector(didTapTitleView:) forControlEvents:UIControlEventTouchUpInside]; 
self.navigationItem.titleView = titleLabelButton; 

2你的標題視圖)實施選擇

- (IBAction)didTapTitleView:(id) sender{ 
    //Perform your actions 
    NSLog(@"Title tap"); 
} 
1

那麼你不能直接具有觸摸title財產UINavigationItemUINavigationBar事件。

有一個屬性titleView,您可以設置和觸摸事件。因此,您可以製作一個UILabel並將其設置爲UINavigationItem

[self.navigationItem setTitleView:label]; 

然後,您可以在此label上設置點擊事件。

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapTitle:)]; 
[label addGestureRecognizer:tap]; 

- (void)didTapTitle:(UILabel *)label { 
    //handle tap... 
} 
0

您需要使用自定義標題視圖併爲其添加輕擊手勢識別器。

viewDidLoad試試這個:

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(navTitleTapped)]; 

UILabel* navigationTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 500, 50)]; 
navigationTitleLabel.text = @"The title goes here..."; 
[navigationTitleLabel addGestureRecognizer:tap]; 

self.navigationItem.titleView = navigationTitleLabel; 

然後,實施navTitleTapped

-(void)navTitleTapped 
{ 
    //do what you need to do 
} 
1
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] 
              initWithTarget:self 
              action:@selector(hideKeyBoard)]; 

    tapGesture.cancelsTouchesInView = NO; 
    [self.navigationController.view addGestureRecognizer:tapGesture]; 


-(void)hideKeyBoard 
{ 
    [self.view endEditing:YES]; 
}