使导航栏标题可单击Objective C iOS 10.2

时间:2017-03-08 15:56:31

标签: ios objective-c uinavigationbar

有没有办法让导航栏标题可点击?我没有任何特定的导航栏类。我从ViewController控制一切。如果有办法,我很想知道。我一直在寻找相当白的但是找不到合适的答案。

2 个答案:

答案 0 :(得分:0)

导航栏上的“制作”按钮

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
           action:@selector(btnclick:)
 forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Title" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, self.view.frame.size.width, 64.0);

self.navigationItem.titleView =button;

-(void)btnclick:(id)sender
{ NSLog(@"button click");
}

答案 1 :(得分:0)

你可以这样试试

UIView *topView = [[UIView alloc]initWithFrame:CGRectZero];

UIButton * button = [[UIButton alloc]initWithFrame:CGRectZero];

[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

[button setTitle:@"Title here" forState:UIControlStateNormal];

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[button sizeToFit];

[topView addSubview:button];

[topView sizeToFit];

self.navigationItem.titleView = topView;

-(void)buttonAction:(Id) sender
{

 NSLog(@"button clicked");

}