Reputation: 656
Setup:
I am working on a view in Xcode 5 that displays a set of textfields. In order to handle scrolling when the keyboard comes up, I am using a scroll view to place my TextFields in. I am using autolayout.
Problem:
I am struggling with an issue where extra space is being added to the top of my scroll view at run time. Like an inset is being applied to my scroll view, though I have confirmed via logging that the insets are zero.
In my viewDidLoad() method implementation I am doing this to confirm values...
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
self.scrollView.contentSize = self.scrollView.bounds.size;
self.scrollView.contentOffset = CGPointMake(0.0, 0.0);
NSLog(@"Content Size: %f x %f", self.scrollView.contentSize.height, self.scrollView.contentSize.width);
NSLog(@"Bounds Size: %f x %f", self.scrollView.bounds.size.height, self.scrollView.bounds.size.width);
NSLog(@"Frame Size: %f x %f", self.scrollView.frame.size.height, self.scrollView.frame.size.width);
NSLog(@"Inset Size top: %f bottom: %f", self.scrollView.contentInset.top, self.scrollView.contentInset.bottom);
NSLog(@"Scroll Inset Size top: %f bottom: %f", self.scrollView.contentInset.top, self.scrollView.contentInset.bottom);
Output:
2013-10-08 11:38:42.953 test[6440:a0b] Content Size: 455.000000 x 320.000000
2013-10-08 11:38:42.954 test[6440:a0b] Bounds Size: 455.000000 x 320.000000
2013-10-08 11:38:42.955 test[6440:a0b] Frame Size: 455.000000 x 320.000000
2013-10-08 11:38:42.955 test[6440:a0b] Inset Size top: 0.000000 bottom: 0.000000
2013-10-08 11:38:42.956 test[6440:a0b] Scroll Inset Size top: 0.000000 bottom: 0.000000
The space(inset) being added above my first textfield seems to be about equal to the standard navigation header, but that could be coincidence.
My scroll view is set to a height of 455 with a y value of 64 in order to fit between the navigation status bar and the tab bar along the bottom.
I do not have any layout notifications of potential problems. The scroll view top and bottom space constraint is positioned to be 0 points (I presume from the status and tab bars.
When running the app and seeing the extra space in the scroll view, the scroll bars are only covering the expected space (they not including this extra space as part of the scrollable area).
One last thing to note. When I perform my keyboard notification logic. (taken from apple here...) The problem resolves itself, no more space. Which makes it seem like an initialization issue with what is configured in the storyboard?
Thanks!
Upvotes: 16
Views: 12121
Reputation: 236
In viewDidLoad method call this line of code:
self.automaticallyAdjustsScrollViewInsets = false
Upvotes: 0
Reputation: 159
In addition to disabling "Adjust Scroll View Insets" on the view controller in IB as indicated by Greg, I found that another way to fix this issue was to edit the "Top Alignment" constraint that I had between the scroll view and the top layout guide. Changing the latter from "Top Layout Guide.Bottom" to "Top Layout Guide.Top" makes things look OK at runtime, although one can still see the extra space in the storyboard (Xcode 6.4).
Upvotes: 0
Reputation: 33650
UIViewControllers in iOS7 have a setting called automaticallyAdjustsScrollViewInsets
. If this is YES
, the scroll view's insets will automatically be adjusted according to the height of the status bar, navigation bar, and toolbar or tab bar.
This setting can also be set in Interface Builder; it's called "Adjust Scroll View Insets" on the Attributes Inspector.
Upvotes: 60