Contents of UIScrollView shifted after dismissing

2019-01-23 09:34发布

问题:

I have a paged scrollview. Each page has a viewcontroller (buttonViewController) that manages a grid of buttons. If you press one of the buttons, the buttonViewController pops up another viewcontroller (detailViewController) modally.

The problem is, when I dismiss the modal view controller, the visible buttonViewController is shifted down by what looks like about 20 pixels. The weird thing is that only the visible page is shifted. If I scroll over to another page that is already loaded, it is still in the correct position.

My question is basically the same as dismissing modalViewController moves main view buttons around iphone

However, the accepted answer to that question assumed that the status bar is hidden. I am not hiding the status bar in my application.

Any other ideas?

One more note: The shift only happens the first time I launch a modal view controller. If I keep opening and closing the modal view controller, everything stays the same.

One further note: if I add the following code:

    CGRect frame = self.view.frame;
frame.origin.y=0;
self.view.frame = frame;

after I dismiss the modal view controller, then I can work around the problem. The frame seems to move by 20pixels in y. I still don't know what's causing the move though.

回答1:

Well I had this problem and it was due to the way my first viewController was set up (on the UIWindow), it is very weird since this had never happened to me before, the behavior I was observing was that when you add a subview at point (0,0) it would overlap with the status bar, which is not usual, usually the views know about the status bar and they behave appropriately. As a side effect, when one pushes a modal view that takes off the status bar, after dismissing it I saw the same problem you are seeing. What I did to fix was to set the UIWindows view to my controllers view in the nib, this seemed to fix everything. Once again I don't know why simply adding to the window programmatically with CGRectMake(0,0,320,460) had this odd behavior, since I have been doing this for all my previous projects and saw the correct behavior. But when I set it up in the nib, it worked as expected. Maybe this is your problem as well.



回答2:

Turns out this was related to: ModalViewController doesn't display during animation when paged scrollView is scrolled

I had mismatched the heights of some of my viewcontrollers, and that was throwing everything off.

Some of the views had heights of 460pixels, some had 480pixels. For some reason, this made it shift by 20pixels evertime a modal view controller disappeared. Once I made everything 460pixels, everything worked great.