Storyboard上でUIScrollView+AutoLayoutを使うと下のようなエラーが出る事があります。
今回はその対処法を調べました。
Xcodeのバージョンは13です。
参考にしたのは下URLです。
まずはプロジェクトを作ってMain.storyboardにUIScrollViewを配置します。
今回はUIScrollViewを縦にだけスクロールするようにしようと思います。
配置したらUIScrollViewに下のような制約を追加します。
制約を追加すると下のようにエラーになり線が赤くなります。
エラー対策の為、まずはUIViewをUIScrollViewの直下に配置します。
次に左側のメニューでViewとContent Layout Guideを選びます。
この状態で下のような制約を追加します。
続けてViewにHeightの制約を追加します。
これでyやheightに関するエラーは消えました。
次はxとwidthに関するエラーの対応をします。
今度はViewとFrame Layout Guideを選びます。
この状態でEqual Widthsの制約を追加します。
これでエラーが消えました。
実行するとスクロールのContentHeightが1000になっている事がわかります。
先程出てきたContent Layout GuideとFrame Layout Guideですが公式サイトに説明がありました。
https://developer.apple.com/documentation/uikit/uiscrollview/2865870-contentlayoutguide
https://developer.apple.com/documentation/uikit/uiscrollview/2865772-framelayoutguide
公式サイトの説明は下の通りです。
要するにContent Layout GuideはUIScrollViewのcontentSizeに関するガイドでFrame Layout GuideはUIScrollViewのframeに関するガイドです。
そのためFrame Layout GuideとViewにEqualWidthsを付ければcontentSize.widthがUIScrollViewのframe.widthと等しくなったのだと思われます。
■ Content Layout Guide Use this layout guide when you want to create Auto Layout constraints related to the content area of a scroll view. ■ Frame Layout Guide Use this layout guide when you want to create Auto Layout constraints that explicitly involve the frame rectangle of the scroll view itself, as opposed to its content rectangle.
続けてContentHeightを動的に変える方法についても見ていきます。
まずはViewControllerにNSLayoutConstraintのIBOutletを作ります。
class ViewController: UIViewController { @IBOutlet var constraint: NSLayoutConstraint! }
Storyboardから今作ったIBOutletとHeightに関するNSLayoutConstraintを紐付けます。
あとはNSLayoutConstraintの値を変えればContentHeightの値が変わります。
class ViewController: UIViewController { @IBOutlet var constraint: NSLayoutConstraint! override func viewDidLoad() { super.viewDidLoad() constraint.constant = 2000 } }