简体   繁体   中英

Scroll view is not scrolling content vertically with AutoLayout in Swift

I have this UIViewController subclass:

class MyCustomViewController: UIViewController, MyDelegate {

   var scrollContainerView: UIView = {
      let scrollContainerView = UIView(frame: CGRectZero)
      scrollContainerView.translatesAutoresizingMaskIntoConstraints = false
      return scrollContainerView
   }()

   var scrollView: UIScrollView = {
      var scrollView = UIScrollView()
      scrollView.translatesAutoresizingMaskIntoConstraints = false
      return scrollView
   }()

   lazy var customView: MyCustomView = {
      var customView = MyCustomView(frame: .zero)
      customView.translatesAutoresizingMaskIntoConstraints = false
      customView.delegate = self
      return customView
   }()

   override func loadView() {
      self.view = UIView()
      self.view.backgroundColor = UIColor.whiteColor()

      self.view.addSubview(self.scrollView)
      self.scrollView.addSubview(self.scrollContainerView)
      self.scrollContainerView.addSubview(self.customView)

      view.setNeedsUpdateConstraints()
   }

   override func updateViewConstraints() {

        self.scrollView.autoPinEdge(.Top, toEdge: .Bottom, ofView: self.view, withOffset: 0)
        self.scrollView.autoPinEdge(.Left, toEdge: .Left, ofView: self.view, withOffset: 0)
        self.scrollView.autoPinEdge(.Right, toEdge: .Right, ofView: self.view, withOffset: 0)
        self.scrollView.autoPinEdge(.Bottom, toEdge: .Bottom, ofView: self.view, withOffset: 0)

        self.scrollContainerView.autoPinEdge(.Top, toEdge: .Bottom, ofView: self.scrollView, withOffset: 0)
        self.scrollContainerView.autoPinEdge(.Left, toEdge: .Left, ofView: self.scrollView, withOffset: 0)
        self.scrollContainerView.autoPinEdge(.Right, toEdge: .Right, ofView: self.scrollView, withOffset: 0)
        self.scrollContainerView.autoPinEdge(.Bottom, toEdge: .Bottom, ofView: self.scrollView, withOffset: 0)

        self.customView.autoPinEdge(.Top, toEdge: .Bottom, ofView: self.scrollContainerView, withOffset: 0)
        self.customView.autoPinEdge(.Left, toEdge: .Left, ofView: self.scrollContainerView, withOffset: 0)
        self.customView.autoPinEdge(.Right, toEdge: .Right, ofView: self.scrollContainerView, withOffset: 0)
        self.customView.autoPinEdge(.Bottom, toEdge: .Bottom, ofView: self.scrollContainerView, withOffset: 0)

       super.updateViewConstraints()
   }
}

Where customView has fixed size. If I run the app in the iPhone 4S simulator, where it is supposed to be needed to scroll to see the complete view, I find that it scrolls horizontally, but not vertically... I don't understand what could I be missing.

It seems that there are two issues in your constraints,

  1. Set scrollContainerView EqualWidth to scrollView as you want your scroll view to be scroll vertically only.

  2. If the customView does not have subViews inside it, it could not able to calculate the height required for it, so you either need to set some points of FixedHeight to the customView or add subviews with top to bottom constraints.

Everything else is fine.

Hope this would help you.

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