UIScrollView's origin changes after popping back to the UIViewController
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;
}
UIScrollView: Scrolling content after ViewController View frame change
I needed to add contentInsets and scrollIndicatorInsets. Great article here... https://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios/
UIScrollView contentOffset change after another view pushed
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.
UIViewController UIKeyInput shows keyboard when popping back
You can override -isFirstResponder
and return NO
(or some custom, conditional logic) from that method in order to prevent such keyboard appearance:
- (BOOL) isFirstResponder {
return NO; // or something else, depending on conditions
}
ScrollView created in storyboard initialized with frame 0,0,0,0
With storyboards viewDidLayoutSubviews is where you can get the size of all your views. viewWillAppear did not work for me and always returned (0,0,0,0);
UIScrollView does not restore properly
Actually I found the answer here:
UIScrollview Autolayout Issue
The exact code that I used is:
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
//save the current offset
previousPoint = scrollView.contentOffset;
//set current view to the beginning point
self.scrollView.contentOffset = CGPointZero;
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
//retrieve the previous offset
self.scrollView.contentOffset = previousPoint;
}
previousPoint
is nothing but a CGPoint variable declared on the implementation.
How do I make UIScrollView remember the position after a Segue is performed?
When you perform a segue, that will push the "new" scene on to your navigation stack. If that scene is subsequently "popped" (which would be the default behavior of the UINavigationController's back button in the new scene), the user should be returned to the original view at the desired content offset automatically. If this is not happening, you may accidentally be pushing a new instance of the original view controller. You should not be performing an additional segue, rather you should be popping the top view controller off of the navigation stack. That said, you can use this rather heavy-handed method for making sure the content offset remains regardless:
Before you perform your segue, set the property to the value of your scrollView's contentOffset property, e.g.:
-(void)prepareForSegue
{
[self setScrollViewOffset:self.scrollView.contentOffset];
}
When this view controller prepares to make another appearance, set it's scrollview's contentOffset:
-(void)viewWillAppear
{
[[self scrollView] setContentOffset:self.scrollViewContentOffset animated:NO];
}
Frame doesn't reflect auto layout constraints after dismissing modal view controller
I created a UIScrollView subclass that works around this issue (which BTW is fixed in iOS7):
@interface ConstraintsSafeScrollView : UIScrollView
@end
@implementation ConstraintsSafeScrollView {
CGPoint _savedContentOffset;
UIEdgeInsets _savedContentInset;
}
- (void)willMoveToWindow:(UIWindow *)newWindow {
if (newWindow) {
// Reset the scrollview to the top.
[super setContentOffset:CGPointMake(-_savedContentInset.left, -_savedContentInset.top)];
}
[super willMoveToWindow:newWindow];
}
// Overridden to store the latest value.
- (void)setContentOffset:(CGPoint)contentOffset {
_savedContentOffset = contentOffset;
[super setContentOffset:contentOffset];
}
// Overridden to store the latest value.
- (void)setContentInset:(UIEdgeInsets)contentInset {
_savedContentInset = contentInset;
[super setContentInset:contentInset];
}
- (void)didMoveToWindow {
if (self.window) {
// Restore offset and insets to their previous values.
self.contentOffset = _savedContentOffset;
self.contentInset = _savedContentInset;
}
[super didMoveToWindow];
}
@end
Related Topics
Randomly Choosing an Item from a Swift Array Without Repeating
Enterprise Deployment Certificate and Profiles
How to Use Git Properly with Xcode
Is There a Public API for Card View UI That Can Be Seen Across iOS 10
Corenfc Not Reading Uid in iOS
How to Change Height of Uitableviewcell Real-Time
How to Round the Corners of a Button
Launch Screen Storyboard Not Displaying Image
@State Var Not Updated as Expected in Lazyvgrid
Core Data Does Not React on Changed Predicate
Over the Air (Ota) iOS IPA File Distribution for Public
Change the Font of a Uibarbuttonitem
Repeat Interval for Unnotification
Images for Retina Screen (@2X)
Uicollectionview Performance - _Updatevisiblecellsnow
Change Uitextfield and Uitextview Cursor/Caret Color