动态内容与预期不工作自动布局的UIScrollView

问题描述 投票:0回答:1

我试图动态内容如下添加到滚动视图

 for i in 0 ..< 4 {
            let gameView = Card.instantiate()
            gameView.frame.origin.y  =  gameView.frame.size.height * CGFloat(i)
            contentView.addSubview(gameView)
            gameView.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
            let widthConstraint = NSLayoutConstraint(item: gameView, attribute: .width, relatedBy: .equal,toItem: contentView, attribute: .width, multiplier: 0.8,constant : 0.0)

         contentView.addConstraint(widthConstraint)

         let heightConstraint = NSLayoutConstraint(item: gameView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 100)

         gameView.addConstraint(heightConstraint)


         contentView.frame.size.height = contentView.frame.size.height + gameView.frame.size.height

         scrollView.contentSize.height = scrollView.contentSize.height + gameView.frame.size.height
        }

Cardview被XIB与自动布局enter image description here定义

和IB视图结构如下面的SuperView>滚动型>内容查看> DynamicViews

内容查看还含有一些静态的内容,如按钮和labels.Dynamic查看低于静态内容

enter image description here

而输出画面看起来像以下但没有正确对齐qazxsw POI

这是添加动态视图以滚动型正确的做法?

swift uiview uiscrollview
1个回答
0
投票

这是enter image description here理想的情况下,

堆栈视图将处理实例UIStackViews的垂直定位和宽度(假设你有你的厦门国际银行加载正确设置)。

通过限制顶部,前置,底部和堆栈视图的宽度与滚动视图,它也将定义“滚动区”(该gameView)。

所有与自动布局完成:

.contentSize
© www.soinside.com 2019 - 2024. All rights reserved.