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:
iOS Autolayout storyboard ScrollView - button bottom of the scrollview is unable to clickable
Just now found the answer with removing any autolayout constraints, for content view we have to set constraints like below:
- Top, bottom, left, right
- Align CenterX - here we have to set the content view height then for that constraint we have to set constant as scrollview content size height
Related Topics
Detect If Swift App Is Being Run from Xcode
Xcode 9 - Localization Issue Warning Storyboard
Using Core Data with Watchos 2.0
How to Manipulate Texture Content on the Fly
Error Appstore Connect:Missing Purpose String in Info.Plist File
The Identity Used to Sign the Executable Is No Longer Valid
Programmatically Focus on a Form in a Webview (Wkwebview)
How Secure Is Nsuserdefaults on iOS 8,9
iOS 11 Uitableview Delete Rows Animation Bug
How to Correctly Subclass Uicontrol
How to Make Both Header and Footer in Collection View with Swift
Xcode 4.2 Jumps to Main.M Every Time After Stopping Simulator
Uiimagepickercontroller Allowsediting Not Working
Why My App Is Not Shown in Icloud Drive Folder
iOS App Won't Run on Device Any More Under iOS 12: Unrecoverable Ct Signature Issue