Reputation: 12509
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.
Upvotes: 1
Views: 1730
Reputation: 4180
It seems that there are two issues in your constraints,
Set scrollContainerView EqualWidth to scrollView as you want your scroll view to be scroll vertically only.
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.
Upvotes: 1