UIScrollView contentOffset change after another view pushed

后端 未结 6 2000
遇见更好的自我
遇见更好的自我 2020-12-31 17:26

I have a UIViewController in Interface Builder, and I add a UIScrollView to the UIView. It has a contentOffset property e

相关标签:
6条回答
  • 2020-12-31 18:00

    Check if the contentSize property of your scrollview is CGSizeZero. I had a similar problem because the contentSize had been not set.

    0 讨论(0)
  • 2020-12-31 18:01

    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!

    0 讨论(0)
  • 2020-12-31 18:02

    For iOS 7/8 self.automaticallyAdjustsScrollViewInsets = NO; solved the problem.

    0 讨论(0)
  • 2020-12-31 18:06

    I was using https://github.com/michaeltyson/TPKeyboardAvoiding on UITableView.

    My solution was to remove class TPKeyboardAvoidingTableView from UITableView. It was causing scroll issue on TableViewController with static and dynamic cells.

    0 讨论(0)
  • 2020-12-31 18:16

    See my answer to a similar question.

    You need to set the scrollview's contentOffset appropriately in viewWillAppear: and viewWillDisappear:.

    Also, see this:

    • This answer to the question UIScrollView's origin changes after popping back to the UIViewController.
    • This related question, UIScrollview Autolayout Issue.
    0 讨论(0)
  • 2020-12-31 18:23

    When you leave the screen where the scrollview is present and come back, it will add half of the size of the screen on top or left or both. A blank space, something you don't want. And in my case, the paging becomes a mess and doesn't work properly, until I put the scroll in the CGPointZero programatically or by touch, as an user. If you call NSLog( @"%f", scrollview.contentSize.height ); on viewDidLayoutSubviews you'll see that the size has changed.

    It happens on the axis which the offset value is not zero. So, to solve it, you should set it to zero CGPointZero before it appears.

    The @Steph Sharp answer solve my problem except if I open a viewcontroller as a popup (presentViewController). I tried to compensate the value by adding the bug amount on viewDidLayoutSubviews but it bugs the paging.

    Short answer, I end up with:

    - (void) viewDidLayoutSubviews
    {
        [super viewDidLayoutSubviews];
        scrollView.contentOffset = previousPoint;
    }
    
    - (void) viewWillAppear:(BOOL)animated
    {
        scrollView.contentOffset = CGPointZero;
    }
    
    - (void) viewWillDisappear:(BOOL)animated
    {
        previousPoint = scrollView.contentOffset;
        scrollView.contentOffset = CGPointZero;
    }
    

    Now it works when I leave the section or if I open a popup above.

    0 讨论(0)
提交回复
热议问题