简体   繁体   English

UIPageViewController和AutoLayout:约束未正确应用

[英]UIPageViewController and AutoLayout: constraints are not applied correctly

I create a UIPageViewController programmatically and add it as a child to my container view-controler as shown below: 我以编程方式创建一个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)

}        

The problem is that the view of the UIPageViewController is not sized correctly as shown in the view hierarchy below. 问题是UIPageViewController的视图大小不正确,如下面的视图层次结构所示。 The view controllers returned by the data-source of the UIPageViewController contains a single UIScrollView . UIPageViewController的数据源返回的视图控制器包含单个UIScrollView I have set the constraints of that UIScrollView such that the scrollView expands to the superview. 我已经设置了UIScrollView的约束,以便scrollView扩展到superview。

It seems to me that the problem stems from the fact that the scrollView constraints are "detached" from the constraints of the container view, but I don't know how to fix it using StoryBoard because these are views in different view-controllers. 在我看来,问题源于scrollView约束与容器视图的约束“分离”的事实,但我不知道如何使用StoryBoard修复它,因为这些是不同视图控制器中的视图。 I am not very familiar with specifying constraints programmatically and my efforts to set the constraints programmatically have failed so far. 我不是很熟悉以编程方式指定约束,到目前为止,我以编程方式设置约束的努力都失败了。

How can I fix this so that the scroll-view of the view-controller of UIPageViewController is properly contained inside the view of the container ViewController? 如何解决这个问题,以便UIPageViewController的视图控制器的滚动视图正确包含在容器ViewController的视图中?

在此输入图像描述

在此输入图像描述

I had to add constraints programmatically to fix the problem. 我必须以编程方式添加约束来解决问题。 Note that I couldn't have added the auto layout constraints using IB because here I'm dealing with two views belonging to two different view-controllers in IB. 请注意,我无法使用IB添加自动布局约束,因为这里我处理的是属于IB中两个不同视图控制器的两个视图。 The views are added programmatically as subviews because they are views of a UIPageViewController . 视图以编程方式添加为子视图,因为它们是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()
}

I had the same problem and it has been solved by adding the code below in viewDidLoad() 我有同样的问题,它已通过在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 that helps someone. 我希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM