Why is UIScrollView leaving space on top and does not scroll to the bottom
1... Why is UIScrollView leaving space on top
With Storyboard- Goto view controller > Attribute Inspector > Uncheck Adjust Scroll View Insets property
With Code- For extra space set viewController
property automaticallyAdjustsScrollViewInsets
to NO
, by default it is YES
.
self.automaticallyAdjustsScrollViewInsets = false;
scroller.contentInset = UIEdgeInsetsZero;
scroller.scrollIndicatorInsets = UIEdgeInsetsZero;
scroller.contentOffset = CGPointMake(0.0, 0.0);
2... does not scroll to the bottom
To make it scroll try with large number in contentSize
like CGSizeMake(320, 1687)
. If it works that means you are not setting the contentSize
large enough to have all its content.
why UIScrollView is leaving space from top in ios 6 and ios 7
In Xcode 5, in storyboard select your controller > in Attribute Inspector disable Adjust Scroll View Insets
Also check if you have set any contentInset
Edit : I attached a pic
UIScrollView & AutoLayout - UIScrollView Over scrolling (showing excessive whitespace at bottom)
To adjust the height of scroll view, you need to do these two things:-
- In the size inspector of scroll view, change the intrinsic size to "PlaceHolder".
- Make sure you have added enough constraints that height of scroll view can be calculated. Since the contents in your scroll view are of static height, you can simply add a height constraint to your scroll view.
Extra 20px Top Inset UIScrollView Issue
Do you have your controller embedded in a navigationController? It seems that iOS is leaving 20px at the top for a status bar but your controller doesn´t have one.
Either way, in your viewDidLoad it should work if you adjust the scrollView before calling super:
- (void)viewDidLoad {
self.automaticallyAdjustsScrollViewInsets = NO;
self.scrollView.contentInset = UIEdgeInsetsMake(0,0,0,0);
[super viewDidLoad];
}
The reason why iOS might be automatically forcing your scrollView to have a 20px TOP contentInset is because it might be the view with index 0 in your main view. You can read further here: http://b2cloud.com.au/how-to-guides/uiviewcontroller-changes-in-ios7
By default, a UIViewController will automatically adjust the content
insets of it’s UIScrollView (including UITableViews) at view index 0,
the very back. This means if there’s a button, label, or any other
view behind your table view, you wont get this behaviour for free.
View at the bottom in a UIScrollView, with AutoLayout
Fairly easy to do with Auto-Layout only... no code required.
The key is to use a "content view" to hold the elements, and a greater-than-or-equal
constraint between your "bottom" element and your "footer" view.
In this image, yellow is the main view, green is the scroll view, blue is the content view, the labels are gray and the footer view is pink.
- Start with a fresh view controller
- add a scroll view, normal constraints (I used
20
all the way around, so we can see the frame) - add a
UIView
to the scrollView - this will be our "content view" - constrain contentView Top/Bottom/Leading/Trailing all equal to
0
to the scrollView - constrain both the Width and Height of the contentView equal to the scrollView
- add your elements - here I used 3 labels
- constrain the labels as usual... I used:
- LabelA - Top/Leading/Trailing all at
20
, vertical spacing to LabelB of60
- LabelB - Leading/Trailing at
20
, vertical spacing to LabelC of60
- LabelC - Leading/Trailing at
20
- LabelA - Top/Leading/Trailing all at
- LabelC is also set to
Number of Lines: 0
so it will expand with multiple lines of text - Add a
UIView
as a "footer view" (I stuck a label in it) - constrain the footerView Leading/Trailing/Bottom all at
20
(so we can see the frame) - either set a Height constraint on footerView, or use its content to constrain its height
- add a Vertical Spacing constraint from LabelC to footerView, and set it to
>= 40
- last step, change the Height constraint of contentView to
Priority: 250
Now, as you expand/contract the height of LabelC, the footerView will keep at least 40-pts of vertical space. When LabelC gets big enough to "push" footerView below the bottom, scrollView will become scrollable.
Results:
UIScrollView starts out halfway down content
Did you try set automaticallyAdjustsScrollViewInsets
to NO
?
Related Topics
What #Defines Are Set Up by Xcode When Compiling for Iphone
Swift Stack and Heap Understanding
Ios7 Status Bar Hide/Show on Select Controllers
Difference Between Presentviewcontroller and Uinavigationcontroller
Setting Scroll Position in Uitableview
How to Save Nsmutablearray in Nsuserdefaults
Linking Objective-C Categories in a Static Library
How to Hide Tab Bar with Animation in iOS
How to Set the Full Width of Separator in Uitableview
How to Dismiss Keyboard iOS Programmatically When Pressing Return
Simply Mask a Uiview with a Rectangle
How to Split Filename from File Extension in Swift
How to Get Commoncrypto/Commoncrypto File From
Automatic Otp Verification in iOS
Uploading Archive Error: "Missing iOS Distribution Signing Identity for ..."