UIScrollView's origin changes after popping ba

2019-01-16 08:38发布

I have a UIViewController subclass as a scene in the storyboard that contains a UIScrollView containing various subviews. One of the subviews is a UIButton which segues into another scene UIViewController subclass. When I come back from the view (pop the UIViewController off the navigation controller stack), I find that the scroll view's origin has somehow changed, although the contentsize and contentoffset seem correct.

What's also interesting is that the app has a tab bar, and when I tab away and back to that view, the scroll view is set back correctly with offset at (0, 0).

There is basically no code involved in this process, as it's pretty much all in the storyboard. As I am fairly new to using the storyboard, I figure I'm doing something wrong, although I don't know what. Any ideas as to what that may be? Perhaps sizing issues or constraints?

16条回答
霸刀☆藐视天下
2楼-- · 2019-01-16 09:30

After trying lots of things:

  • Set automaticallyAdjustsScrollViewInsets to false to see if the problem was with the navigation bar.
  • Playing with fixed cell heights...
  • Caching the cell heights...
  • Keeping a property with the table view offset, caching it and setting it on viewWillAppear...
  • etc.

I realised the issue was about the estimatedRowHeight value, which was set to 50px, when it should be ~150px. Updating the value fixed the issue and now the table view keeps the same offset.

查看更多
Animai°情兽
3楼-- · 2019-01-16 09:31

i had a similar problem, after dismissing a viewController, the contentOffset from my tableView was changed to (0, -64).

my solution was a little weird, i tried all the other answers but had no success, the only thing that fixed my problem was to switch the tableView position in the controls tree of the .xib

it was the first control in the parent View like this:

before

I moved the tableView right after the ImageView and it worked:

after

it seems that putting the table view in the first position was causing the trouble, and moving the table view to another position fixed the problem.

P.D. I'm not using autoLayout neither storyboards

hope this can help someone!

查看更多
闹够了就滚
4楼-- · 2019-01-16 09:32

I posted a question recently about a similar issue but in a different context: Frame doesn't reflect auto layout constraints after dismissing modal view controller

In my case, the origin of a custom container view inside the scroll view is displaced, rather than the origin of the scroll view itself. With respect to auto layout, the custom container view is pinned to the four sides of the scroll view.

The container view is created and configured programmatically rather than in IB. Although the constraints of the container view are unchanged, the container view's origin is displaced after dismissing a modal view controller. This disconnect between constraints and frames is inexplicable.

What's even more remarkable is that the origin displacement problem remained after I removed and re-added all related constraints.

I came up with a similar solution: save the value for the current content offset, set the content offset to "zero", let the system swap out your views, then restore the content offset (i.e., a content-offset dance).

In my case, however, I had to take additional steps to resolve the issue. My scroll view is a paging scroll view that gets its subviews from a tilePages method (demoed in an old WWDC video). Changing the scroll view's content offset triggers the tilePages method via the UIScrollViewDelegate's scrollViewDidScroll: method. I had to set a flag to turn off tilePages while I did the content-offset dance, otherwise the app would crash.

Hopefully, I can remove the code for the content-offset dance when I upgrade to iOS 7.

查看更多
淡お忘
5楼-- · 2019-01-16 09:33

Try this in viewWillAppear of the view controller you pop back into:

 self.scrollView.contentOffset = CGPointMake(0, 0);

Edit: When also adding Peter's code you get the best results with:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.scrollView.contentOffset = CGPointMake(0, 0);
}

plus

- (void)viewWillDisappear:(BOOL)animated {  
    self.recentContentOffset = self.scrollView.contentOffset;
    [super viewWillDisappear:animated];
}

and

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.scrollView.contentOffset = CGPointMake(0, self.recentContentOffset.y);
}

You return to the original scroll position, have no visual side-effect and the scroll view itself is correctly positioned in its superview (which was a problem) after coming back.

查看更多
一纸荒年 Trace。
6楼-- · 2019-01-16 09:35

I used a combination of the different solutions posted everywhere on SO, and came up with this subclass:

// Keeps track of the recent content offset to be able to restore the
// scroll position when a modal viewcontroller is dismissed
class ScrollViewWithPersistentScrollPosition: UIScrollView {

    // The recent content offset for restoration.
    private var recentContentOffset: CGPoint = CGPoint(x: 0, y: 0)

    override func willMove(toWindow newWindow: UIWindow?) {
        if newWindow != nil {
            // save the scroll offset.
            self.recentContentOffset = self.contentOffset
        }
        super.willMove(toWindow: newWindow)
    }

    override func didMoveToWindow() {
        if self.window != nil {
            // restore the offset.
            DispatchQueue.main.async(execute: {
                // restore it
                self.contentOffset = self.recentContentOffset
            })
        }
        super.didMoveToWindow()
    }
 }

Tested on iOS 11.2 / Xcode 9.2.

查看更多
放荡不羁爱自由
7楼-- · 2019-01-16 09:36

Actually, I put that line of code in viewDidDisappear, and so that it remembers the offset when the view reappears, I added this line before it

 self.contentOffset = self.scrollView.contentOffset;

as well as

 - (void)viewDidLayoutSubviews { 
       self.scrollView.contentOffset = self.contentOffset; 
 }
查看更多
登录 后发表回答