1

我在子类中有以下viewDidLoad方法实现UIViewController

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)
}

但是当我运行应用程序时,内容不会滚动。我发现很少有PureLayout文档和示例,并且对滚动视图一无所知。有人可以帮我解决这个问题吗?

4

2 回答 2

6

当您将自动布局与 Scrollview 一起使用时,您必须确保 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))
于 2016-08-19T23:10:39.583 回答
0

这可能会帮助某人,

func updateUI(){



    self.view.backgroundColor = .white
    self.view.addSubview(scrollView)
    scrollView.addSubview(contentView)
         contentView.addSubview(offer_title)
         contentView.addSubview(offer_image)
         contentView.addSubview(body)

    scrollView.autoPinEdgesToSuperviewEdges()
    contentView.autoPinEdgesToSuperviewEdges()
    contentView.autoMatch(.width, to: .width, of: view)



         offer_image.contentMode = .scaleToFill
         offer_image.autoPinEdge(toSuperviewEdge: .top, withInset: 0)
         offer_image.autoPinEdge(toSuperviewEdge: .trailing, withInset: 0)
         offer_image.autoPinEdge(toSuperviewEdge: .leading, withInset: 0)
         offer_image.autoSetDimension(.height, toSize: 300)

         offer_title.autoPinEdge(.top, to: .bottom, of: offer_image, withOffset: 8)
         offer_title.autoPinEdge(toSuperviewEdge: .trailing, withInset: 8)
         offer_title.autoPinEdge(toSuperviewEdge: .leading, withInset: 8)
         offer_title.autoSetDimension(.height, toSize: 60)
         body.numberOfLines = 0
         body.font = UIFont(name: "HelveticaNeueLT-Regular", size: 20)
         body.autoPinEdge(.top, to: .bottom, of: offer_title, withOffset: 8)
         body.autoPinEdge(toSuperviewEdge: .trailing, withInset: 8)
         body.autoPinEdge(toSuperviewEdge: .leading, withInset: 8)
         body.autoPinEdge(toSuperviewEdge: .bottom, withInset: 8)
     }

只需在viewdidLoad中调用它

于 2020-03-29T09:43:56.013 回答