Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Scroll View in Swift 5?

I wanna add scroll view in my project but in swift 5 I can't add it I tried many ways

@IBOutlet weak var scrollView: UIScrollView! 
@IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
@IBOutlet weak var imageViewLeadingConstraint: NSLayoutConstraint!
@IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
@IBOutlet weak var imageViewTrailingConstraint: NSLayoutConstraint!

enter image description here

like image 823
Ahmad Mustafa Avatar asked Nov 26 '19 08:11

Ahmad Mustafa


3 Answers

1.Add scrollView(1) in storyboard, add needed constraint to top/bottom/trailing/leading.

2.Then uncheck "Content Layout Guides" in Size inspector section for your scrollView.

3.Then you need to put into your scrollView new UIView(2), its constraints are for top/bottom/trailing/leading to superView(1) and equal width to superView(1).

4.Then the height of your view(2) you can add as constraint or you can add the content, which will give the height to your view(2).

Example with the height setted

enter image description here

Example with the content, which give the height to your view(2)

enter image description here

like image 156
kseniiap Avatar answered Sep 21 '22 01:09

kseniiap


It works 100% when I Disabling the content layout guides in properties

enter image description here

like image 45
Ahmad Mustafa Avatar answered Sep 22 '22 01:09

Ahmad Mustafa


If you use interface builder first of all add your scroll view and set scroll view constraint (top, bottom, leading and trailing) as (0,0,0,0). View hierarchy must be like this view -> scroll view -> view(content view). Again add constraints for your content view. Content view must have equal width and equal height with parent view (Scroll View). Control drag from content view to scroll view in Document Outline

For more information you can look at this https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithScrollViews.html

like image 31
Emre Değirmenci Avatar answered Sep 19 '22 01:09

Emre Değirmenci