I have a UIViewController
subclass as a scene in the storyboard that contains a UIScrollView
containing various subviews. One of the subviews is a
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.
So, I reset the collectionView's constraints in the Storyboard, making certain they were pinned to presentingViewController's main view.
Added: self.view.translatesAutoresizingMaskIntoConstraints = YES;
inside the viewDidLoad
of the presenting view controller.
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;
}
- (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!
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
.
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!
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.
In iOS 7/8/9 simple self.automaticallyAdjustsScrollViewInsets = NO;
solved the problem in my case.