[swift4] UIScrollViewがAutoLayoutで配置するとスクロールしない問題

環境:Xcode9 swift4

UIScrollViewがAutoLayoutで配置するとスクロールしない問題は下記の通りで解決する。

    @IBOutlet var scrollView: UIScrollView!
    @IBOutlet var baseView: UIView!

 override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        let contentWidth = scrollView.bounds.width
        let contentHeight = baseView.bounds.height*1.5
        scrollView.contentSize = CGSize(width: contentWidth, height: contentHeight)
    }

scrollViewの上にbaseViewを乗せて、baseViewの上に各材料を乗せている

*scrollViewとbaseViewは上下右左のMarginを0という成約でAutoLayoutしている。

viewDidLayoutSubviewsでAutolayout要素を上書きできる。

scrollViewがこれで変更になっても、scrollViewを参照してAutolayoutで配置している他のオブジェクトはいい感じに調整してくれる。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です