[英]Scroll view is not scrolling content vertically with AutoLayout in Swift
我有这个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 模拟器中运行应用程序,在那里应该需要滚动才能看到完整的视图,我发现它水平滚动,而不是垂直滚动......我不明白我会错过什么。
看来你的约束有两个问题,
将 scrollContainerView EqualWidth 设置为 scrollView 因为您希望滚动视图仅垂直滚动。
如果customView里面没有subViews,它就无法计算出它需要的高度,所以你需要给customView设置FixedHeight的一些点,或者添加具有上下约束的子视图。
其他一切都很好。
希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.