【发布时间】:2016-03-20 16:23:48
【问题描述】:
我有这个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 模拟器中运行该应用程序,它应该需要滚动才能看到完整的视图,我发现它水平滚动,但不是垂直滚动......我不明白我会错过什么。
【问题讨论】:
-
你还需要给 self.view 添加 self.scrollContainerView 约束。
-
@ArunGupta 它对我不起作用...
标签: ios swift uiscrollview autolayout