2015-04-01 4 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) Реализовать Selector

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

Ну вы не можете напрямую иметь контакт событие на title имущество UINavigationItem от UINavigationBar.

Существует свойство 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]; 
} 
Смежные вопросы