UIScrollView's origin changes after popping back to the UIViewController

后端 未结 17 2219
不知归路
不知归路 2020-12-07 16:50

I have a UIViewController subclass as a scene in the storyboard that contains a UIScrollView containing various subviews. One of the subviews is a

相关标签:
17条回答
  • 2020-12-07 17:02

    Continued Issue when following the current answers:

    The second attempt to open the presented view controller, without having left the presenting view controller, the problem remained. Which is why I am posting the exact steps that resulted in my solution.

    1. So, I reset the collectionView's constraints in the Storyboard, making certain they were pinned to presentingViewController's main view.

    2. Added: self.view.translatesAutoresizingMaskIntoConstraints = YES; inside the viewDidLoad of the presenting view controller.

    3. And stored, privately, the contentOffset of the collection view prior to the modally presented view controller's appearance:

      (void)viewWillDisappear:(BOOL)animated
      {
          [super viewWillDisappear:animated];
      
          self.contentOffset = self.collectionView.contentOffset;
          self.collectionView.contentOffset = CGPointZero;
      }
      
      - (void)viewDidLayoutSubviews {
           [super viewDidLayoutSubviews];
           self.collectionView.contentOffset = self.contentOffset;
      }
      
    0 讨论(0)
  • 2020-12-07 17:02
    - (void)viewDidDisappear:(BOOL)animated {
        [super viewDidDisappear:animated];
    
        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
            _isRecoredforios6 = YES;
            _recordedOffsetforios6 = _scrollView.contentOffset;
            _recordedSizeforios6 = _scrollView.contentSize;
            _scrollView.contentOffset = CGPointZero;
        }
    
    }
    
    - (void)viewDidLayoutSubviews {
        [super viewDidLayoutSubviews];
    
        if (_isRecoredforios6) {
            _isRecoredforios6 = NO;
            _scrollView.contentSize = _recordedSizeforios6;
            _scrollView.contentOffset = _recordedOffsetforios6;
        }
    }
    

    I have fixed this ios6 bug , can be solved using these codes. I solved bug occurred in Scrollview. Thank above all my friends!

    0 讨论(0)
  • 2020-12-07 17:03

    Unfortunately, Peter and MacMark's suggestions did not work for me (Xcode 5 w/ auto-layout). The solution was to go to the storyboard, select the view controller, and Reset to Suggested Constraints in View Controller.

    enter image description here

    0 讨论(0)
  • 2020-12-07 17:03

    recently , I have encountered this bug, can be solved using these codes:

    // fix ios 6 bug begin
    - (void)viewDidDisappear:(BOOL)animated
    {
        [super viewDidDisappear:animated];
    
        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7) {
            isRecoredforios6 = YES;
            recordedOffsetforios6 = self.tableView.contentOffset;
            recordedSizeforios6 = self.tableView.contentSize;
        }
    }
    
    - (void)viewDidLayoutSubviews
    {
        [super viewDidLayoutSubviews];
        if (isRecoredforios6) {
            isRecoredforios6 = NO;
            self.tableView.contentSize = recordedSizeforios6;
            self.tableView.contentOffset = recordedOffsetforios6;
        }
    }
    // fix ios 6 bug end
    

    thanks Peter Jacobs!

    0 讨论(0)
  • 2020-12-07 17:07

    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.

    0 讨论(0)
  • 2020-12-07 17:08

    In iOS 7/8/9 simple self.automaticallyAdjustsScrollViewInsets = NO; solved the problem in my case.

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