0

I have a view controller, which is presented using pushViewController. This methods presents the view controller with a navigation bar, with a back button on the left side (this is a cocoa method, created by apple).

I want the back button to do just what it does right now, but I also want to tweak some things with it's animation.

How can I add code which works between the button being pressed and the actual animation and return?

thank you.

1 Answer 1

0

I do similar stuff by adding my custom back button like this:

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

And then handle user taps and add my own stuff here:

- (void)backAction {
   // My own handlings
}
1
  • the problem is that I don't really know what happens under the hood when the "original" button is pressed, and because of this it is difficult to recreate in the new button
    – Nitzan R
    Commented Oct 7, 2015 at 12:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.