-viewWillAppear: on iOS7

Guides | Tutorial By 4 years ago

The swipe from left to go back interaction newly added in iOS7 can be a bit of a pain to code to in the beginning…

Because you interact via a drag, the user can choose to cancel going back and drag it back to it’s original position. Behind all this, -viewWillAppear: is still called on your other view controller. Some developers may first encounter some strange bugs when they use the new interaction compared to the old tap back button action, most likely from code that is expecting something to definitely happen but it never does.

At WWDC this year, even the Apple engineers joked around a bit saying it would have been more appropriate if it was now named -viewMightAppear:. Some also mentioned a couple of their apps broke because of the view controllers expecting something which never happened, so you’re not alone 🙂

When -viewWillAppear: is called, you may want to check if it is because of a drag interaction or from the back button being tapped. It is very simple to do:

- (void) viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];

	const BOOL isInteractivePop = (self.navigationController.interactivePopGestureRecognizer.state == UIGestureRecognizerStateBegan);

	if(isInteractivePop)
	{
		//	Interactive pop stuff
	}
	else
	{
		//	Back button stuff
	}
}