UIPageViewController and AutoLayout: Constraints are not being applied correctly
I am programming code UIPageViewController
and adding it as a child to my controller container as shown below:
override func viewDidLoad() {
super.viewDidLoad()
self.pageViewController = UIPageViewController(transitionStyle:.PageCurl, navigationOrientation:.Horizontal, options: nil)
self.mainImageView!.userInteractionEnabled = true
self.pageViewController.delegate = self
self.pageViewController.dataSource = self.modelController
self.addChildViewController(self.pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}
The problem is that the view is UIPageViewController
not the correct size as shown in the view hierarchy below. The view controllers returned by the data source UIPageViewController
contain one UIScrollView
. I have set limits on this UIScrollView
so the scrollView expands to supervisor.
It seems to me that the problem is related to the scrollView constraints being "decoupled" from the container's view constraints, but I don't know how to fix this with StoryBoard because they are views in different views, controllers. I am not very well versed in software limitations of programs, and my attempts to set limitations programmatically failed.
How can I fix this so that the scrolling view of the view controller is UIPageViewController
properly positioned inside the container's ViewController?
source to share
I had to add constraints programmatically to fix the problem. Please note, I could not add auto-layout constraints with IB because here I am dealing with two views that belong to two different view controllers in IB. Views are added programmatically as subzones as they represent a view UIPageViewController
.
override func viewDidLayoutSubviews() {
self.pageViewController.view.setTranslatesAutoresizingMaskIntoConstraints(false)
// Equal height constraint
let constraint = NSLayoutConstraint(item: self.mainImageView!, attribute: .Height, relatedBy: .Equal, toItem: self.pageViewController.view, attribute: .Height, multiplier: 1.0, constant: 0)
self.view.addConstraint(constraint)
// Equal width constraint
let constraint1 = NSLayoutConstraint(item: self.mainImageView!, attribute: .Width, relatedBy: .Equal, toItem: self.pageViewController.view, attribute: .Width, multiplier: 1.0, constant: 0)
self.view.addConstraint(constraint1)
// Equal top constraint
let constraint2 = NSLayoutConstraint(item: self.mainImageView!, attribute: .Top, relatedBy: .Equal, toItem: self.pageViewController.view, attribute: .Top, multiplier: 1.0, constant: 0)
self.view.addConstraint(constraint2)
self.view.layoutSubviews()
}
source to share
I had the same problem and solved it by adding below code in viewDidLoad()
pageViewController!.view.setTranslatesAutoresizingMaskIntoConstraints(false)
let views = ["pageView": pageViewController!.view]
var constH = NSLayoutConstraint.constraintsWithVisualFormat("H:|[pageView]|", options: .AlignAllCenterY, metrics: nil, views: views)
view.addConstraints(constH)
var constW = NSLayoutConstraint.constraintsWithVisualFormat("V:|[pageView]|", options: .AlignAllCenterX, metrics: nil, views: views)
view.addConstraints(constW)
I hope this helps someone.
source to share