滚动视图不会在Swift中使用AutoLayout垂直滚动内容

时间:2016-03-20 16:23:49

标签: ios swift uiscrollview autolayout

我有这个UIViewController子类:

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

customView的大小固定。如果我在iPhone 4S模拟器中运行应用程序,它应该需要滚动查看完整视图,我发现它是水平滚动,但不是垂直滚动...我不明白我能做什么丢失。

1 个答案:

答案 0 :(得分:1)

您的约束中似乎存在两个问题,

  
      
  1. 将scrollContainerView EqualWidth设置为scrollView,因为您希望滚动视图仅垂直滚动。

  2.   
  3. 如果customView中没有子视图,则无法计算所需的高度,因此您需要设置   FixedHeight的一些点到customView或从上到下添加子视图   约束

  4.   

其他一切都很好。

希望这会对你有所帮助。