Swift基礎:ScrollView in Storyboard

王彤
王彤
Sep 8, 2018 · 2 min read

剛開始學Swift時有學到ScrollView,最近工作需要又碰到了ScrollView,才發現原來我根本沒學會,經過Google還有請教前輩以後終於學會了,在此整理給大家!


1.拉一個ScrollView到ViewController,然後跟邊界頂滿

2.ScrollView不知道自己多寬多高,必須要靠他裡面的元件告訴他,關鍵就在於:『頂好頂滿』!我的做法是給他一個ContainerView,告訴他要多寬多高。

ContainerView丟進ScrollView以後,上下左右頂滿,高度先隨便給他800,寬度就讓ContainerView equal Widths to view,這樣ScrollView就知道自己的高度跟寬度了。

3.開始把想要的東西丟進ContainerView

不管丟進去的東西有多少,都要頂好頂滿,最後只要給其中一個固定高度就可以了。
我丟3個view進去,紅色跟綠色的view除了bottom都頂滿,藍色就要上下左右都頂滿,並且給藍色一個固定高500,綠色固定高100。

4.auto layout完成!

To be continued……

Tung's iOS

紀錄Tung的iOS學習之路

王彤

Written by

王彤

Tung's iOS

紀錄Tung的iOS學習之路

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade