简体   繁体   中英

Set UIScrollView content dynamically with Autolayout

I am using Autolayout with UIScrollView in XCode 9. I wish to have a horizontal paging scrollview with 2-3 pages. I have set a ContentView as the single childview and put constraints on all edges so it aligns with the scroll view. The PageViews are then children of the ContentView.

  UIView
    UIScrollView
       ContentView
          PageView1
          ....

This works great and autolayout works for the scrollview. However I wish to change the content view size, sometimes 2 pages otherwise 3. I have an initial constraint on the ContentView width of twice the outer UIView width with priority set to 250.

If at runtime I change the ContentView frame so the width is increased, this does not get set and the ContentView remains at its original size, I imagine iOS is changing the frame back.

Anyone know how to do this?

首先,我建议针对此问题使用UICollectionView / UIStackView ,其次更改框架宽度将无济于事,附加的每个项目都应对前一个尾迹,top&bottom&trailing到contentView具有前导约束,然后scrollview将自动调整大小以显示添加的项目

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM