iOS - 以编程方式添加的视图的自动约束

时间:2015-02-27 18:05:09

标签: ios swift autolayout uipageviewcontroller

我尝试以编程方式在屏幕顶部的自定义页面控件下添加UIPageViewController,但填写了屏幕的其余部分。当页面最初加载时我的工作原理,但是当设备旋转时,它不会重新调整其大小以仅填充屏幕(即,约束仅适用于初始加载)。我在这里缺少什么?

override func viewDidLoad()
{
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ResultsPageView") as? UIPageViewController

    self.loadActivityIndicator.startAnimating()
    self.topPageControl.hidden = true

    self.pageViewController!.view.frame = CGRectZero

    self.addChildViewController(self.pageViewController!)
    self.view.addSubview(self.pageViewController!.view)

    self.pageViewController!.didMoveToParentViewController(self)

    let constraintTop = NSLayoutConstraint(item: pageViewController!.view, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: topPageControl, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
    let constraintBottom = NSLayoutConstraint(item: pageViewController!.view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.bottomLayoutGuide, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0)
    let constraintLeading = NSLayoutConstraint(item: pageViewController!.view, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0)
    let constraintTrailing = NSLayoutConstraint(item: pageViewController!.view, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0)

    self.view.addConstraint(constraintTop)
    self.view.addConstraint(constraintBottom)
    self.view.addConstraint(constraintLeading)
    self.view.addConstraint(constraintTrailing)
    self.pageViewController!.view.setTranslatesAutoresizingMaskIntoConstraints(false)
}

0 个答案:

没有答案