无法使用PureLayout设置滚动视图

App*_*Dev 0 uiscrollview ios autolayout swift pure-layout

viewDidLoadUIViewController子类中有以下方法实现:

var scrollView  = UIScrollView.newAutoLayoutView()
var contentView = UIView.newAutoLayoutView()

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(scrollView)
    scrollView.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsZero)

    scrollView.addSubview(contentView)
    contentView.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsZero)
    contentView.autoMatchDimension(.Height, toDimension: .Height, ofView: view)
    contentView.autoMatchDimension(.Width, toDimension: .Width, ofView: view)

    contentView.addSubview(customView)
    customView.autoPinEdgeToSuperviewEdge(.Top, withInset:0)
    customView.autoPinEdgeToSuperviewEdge(.Left, withInset:15)
}
Run Code Online (Sandbox Code Playgroud)

但是,当我运行应用程序时,内容不会滚动.我发现很少有PureLayout文档和示例,并且没有关于滚动视图的清楚.有人可以帮我这个吗?

gmo*_*mes 5

当您使用Scrollview的autolayout时,您必须确保contentView中的每个视图都固定到所有4个边.

你的customView只固定到TOP和LEFT,尝试固定到所有方面并使用autoSetDimension(.Height,toSize:1300)到一个大尺寸并观察它的工作:)

这是一个有效的示例代码

    // background
    view.backgroundColor = UIColor.lightGrayColor()

    // ScrollView setup
    let scrollView = UIScrollView()
    scrollView.backgroundColor = UIColor.blueColor()
    view.addSubview(scrollView)
    scrollView.autoPinEdgesToSuperviewEdges()

    let scrollContentView = UIView()
    scrollContentView.backgroundColor = UIColor.redColor()
    scrollView.addSubview(scrollContentView)
    scrollContentView.autoPinEdgesToSuperviewEdges()
    scrollContentView.autoMatchDimension(.Width, toDimension: .Width, ofView: view)

    let dummy = UIView()
    dummy.backgroundColor = UIColor.whiteColor()
    scrollContentView.addSubview(dummy)
    dummy.autoSetDimension(.Height, toSize: 1300)
    dummy.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsMake(20, 20, 20, 20))
Run Code Online (Sandbox Code Playgroud)