How to reset my UIScrollView's position after returning from a modal transition?

前端 未结 6 837
-上瘾入骨i
-上瘾入骨i 2021-02-07 12:46

I have a simple view containing a long view with many buttons, with the whole thing being in a UIScrollView. The scroller works well, and I can s

6条回答
  •  情深已故
    2021-02-07 13:16

    Use below code snippet to restore the scroll position for a UIScrollview

    Declare "scrollPosition" variable as CGPoint type.

    - (void)viewDidDisappear:(BOOL)animated 
    {
        [super viewDidDisappear:animated];
        //get the current offset
        scrollPosition = scrollView.contentOffset;
    
        //set current view to the beginning point
        self.scrollView.contentOffset = CGPointZero;
    }
    
    - (void)viewDidLayoutSubviews 
    {
        [super viewDidLayoutSubviews];
        //retrieve the previous offset
        self.scrollView.contentOffset = scrollPosition;
    }
    

提交回复
热议问题