This question discusses how to create gaps between views in a UIScrollView. The agreed method is to increase the width of UIScrollView's frame to create gaps between the views which will be scrolled past. However when the scroll view is a subview of a UINavigationController an issue arises. The UINavigationController changes the frame of the UIScrollView after it has been set up in -viewDidLoad
. The solution proposed is to alter the frame size in -viewDidAppear
, at which point UINavigationController has finished changing things and the alterations will stick.
- (void)viewDidAppear:(BOOL)animated {
CGRect frame = self.view.frame;
frame.size.width += 20; // This gives a 20px gap width
self.view.frame = frame;
}
I've been using this in my application and it has been working fine, but I run into problems when the app is sent to the background and then retrieved again. It seems that UINavigationController changes the frame once again, resetting it to the default width, but this time -viewDidAppear
is not called and so the alteration is never made. I've tried registering for UIApplicationWillEnterForegroundNotification
and UIApplicationDidBecomeActiveNotification
but both of these seem to occur before UINavigationController changes things, so altering the frame at this point has no effect.
At what point should I try to change the frame size such that it will stick, or can I somehow prevent UINavigationController from altering the frame in the first place?
EDIT:
So I subclassed UIScrollView and found that a method called [UIViewControllerWrapperView setFrame:]
is being called which is in turn calling my subclass's -setFrame:
method to set the frame size. I don't know what UIViewControllerWrapperView
is or does and I'm not sure it is going to be possible to jump in after it has done its thing and set the frame size properly. I will continue to investigate.