如何覆盖pushViewController创建的导航栏的功能

时间:2015-10-07 11:58:39

标签: ios ipad ios9 viewcontroller xcode7

我有一个视图控制器,使用pushViewController显示。这个方法为视图控制器提供了一个navigation bar,左侧有一个后退按钮(这是一个由apple创建的可可方法)。

我希望后退按钮可以完成它现在所做的事情,但我也想用它的动画来调整一些东西。

如何添加在按下的按钮和实际动画之间起作用的代码并返回?

谢谢。

1 个答案:

答案 0 :(得分:0)

我通过添加我的自定义后退按钮来做类似的事情:

UIImage *backArrow = [UIImage imageNamed:backButtonImage];
UIButton *aBackButton = [UIButton buttonWithType:UIButtonTypeSystem];
CGSize aBackButtonTextSize = [@"Back" sizeWithFont:[UIFont systemFontOfSize:17]];
aBackButton.frame = CGRectMake(0.0, 0.0, aBackButtonTextSize.width + backArrow.size.width, self.navigationController.navigationBar.frame.size.height);
aBackButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
SEL backSelector = NSSelectorFromString(@"backAction");
[aBackButton addTarget:self action:backSelector forControlEvents:UIControlEventTouchUpInside];
[aBackButton setTitle:@"Back" forState:UIControlStateNormal];
[aBackButton setImage:backArrow forState:UIControlStateNormal];
[aBackButton setExclusiveTouch:YES];

aBackButton.titleLabel.font = [UIFont systemFontOfSize:17];

UIBarButtonItem *aLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aBackButton];
UIBarButtonItem *aNegativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
aNegativeSpacer.width = -8;
self.navigationItem.leftBarButtonItems = @[aNegativeSpacer, aLeftBarButtonItem];

然后处理用户点击并在此处添加我自己的内容:

- (void)backAction {
   // My own handlings
}