简体   繁体   中英

How do I preserve the frame of a UIScrollView upon entering foreground?

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.

I solved this problem by making the UIScrollView a subview of my View Controller's view. As a result, the UINavigationController leaves the UIScrollView alone and the frame is preserved. This also had the added benefit of allowing me to apply the padding in the same method I use to set up the scroll view, instead of later on in -viewDidLoad .

So in summary, I've done this:

CGRect frame = CGRectMake(0, 0, 320, 460);
scrollView = [[UIScrollView alloc] initWithFrame:frame];

// I do some things with frame here

CGRect f = scrollView.frame;
f.size.width += PADDING; // PADDING is defined as 20 elsewhere
scrollView.frame = f;

[self.view addSubview:scrollView];

Try doing it in viewDidLayoutSubviews. If that doesn't work, I would subclass the UIScrollView and override setFrame to add the required gap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM