1

I have the following viewDidLoad method implementation in a UIViewController subclass:

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

But content is not scrolled when I run the app. I find few PureLayout documentation and examples, and nothing clear about scroll views. Could somebody help me with this?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
AppsDev
  • 12,319
  • 23
  • 93
  • 186

2 Answers2

7

When you use autolayout with Scrollview, you have to make sure every view inside your contentView is pinned to all 4 sides.

You customView is only pinned to TOP and LEFT, try pinning to all sides and use the autoSetDimension(.Height, toSize: 1300) to a big size and watch it work :)

here is a sample code that is working

    // 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))
gmogames
  • 2,993
  • 1
  • 28
  • 40
0

This May Help someone,

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

and just call it in viewdidLoad

Mohammed Riyadh
  • 883
  • 3
  • 11
  • 34