David David - 3 months ago 7
iOS Question

Xcode; Set height of UIScrollView automatically depending in its content

I'm developing an iOS app with Xcode and Swift.

In one

ViewController
there's a
UIScrollView
. In this
ScrollView
there are multiple
UITextFields
and
UILabels
. But it takes more height that it's fitting on the device screen and I'm not able to scroll down to see the remaining content.

Does anybody know a way to set
UIScrollViews
height automatically depending in its content?

P.S. Of cause, I already googled, but the codes I found don't work.

Answer

Decleare outlet like and connect:

@IBOutlet var scrollView: UIScrollView?

Set its content size using contentSize method like this:

self.scrollView!.contentSize = CGSize(width:1234, height: 5678)

Set Y position of last UI content plus its height too to the height: 5678 parameter. So you will be able to see all contents.

set your constraints accordingly see image. enter image description here