Raw*_*ean 5 ios uipageviewcontroller autolayout
我UIPageViewController以编程方式创建一个并将其作为子项添加到我的容器视图控制器中,如下所示:
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)
}
Run Code Online (Sandbox Code Playgroud)
问题在于视图的UIPageViewController大小不正确,如下面的视图层次结构所示。的数据源返回的视图控制器包含UIPageViewController单个UIScrollView. 我已经设置了约束,UIScrollView以便滚动视图扩展到超级视图。
在我看来,问题源于这样一个事实:scrollView 约束与容器视图的约束“分离”,但我不知道如何使用 StoryBoard 修复它,因为这些是不同视图控制器中的视图。我不太熟悉以编程方式指定约束,并且迄今为止我以编程方式设置约束的努力都失败了。
如何解决此问题,以便视图控制器的滚动视图UIPageViewController正确包含在容器 ViewController 的视图内?


我必须以编程方式添加约束来解决问题。请注意,我无法使用 IB 添加自动布局约束,因为这里我正在处理属于 IB 中两个不同视图控制器的两个视图。这些视图以编程方式添加为子视图,因为它们是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()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6009 次 |
| 最近记录: |