Calculating contentSize for UIScrollView when using Auto Layout

前端 未结 4 1883
深忆病人
深忆病人 2020-12-12 17:20

Here is a quick question about something which works, but could be written much better. I have a UIScrollView and a list of objects laid out inside, one under t

4条回答
  •  既然无缘
    2020-12-12 18:20

    I also used constraints programmatically to dynamically modify UIScrollView's contentSize . I pretty much followed Shrikant's instructions but for step 2.1, I set centerX to the scrollViews.centerX rather than set the leading and trailing margins (No idea why the former works, while the latter doesn't). Then as a last step, after adding the final subview, I did the following:

    contentView.layoutIfNeeded() //set a frame based on constraints
    scrollView.contentSize = CGSize(width: contentView.frame.width, height: contentView.frame.height)
    

    Hope this helps somebody in the future.

提交回复
热议问题