2014-09-01 67 views
3

我創建了一個UIPageViewController編程並將其添加爲一個孩子我的容器視圖CONTROLER如下圖所示: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) 

}   

的問題是,UIPageViewController的觀點大小不正確,如下面的視圖層次結構所示。由UIPageViewController的數據源返回的視圖控制器包含一個UIScrollView。我已經設置了UIScrollView的限制,以便scrollView擴展到超級視圖。

在我看來,問題源於scrollView約束與容器視圖的約束「分離」,但我不知道如何使用StoryBoard修復它,因爲這些視圖是在不同視圖中的視圖-controllers。我不是很熟悉以編程方式指定約束,並且我迄今爲止以編程方式設置約束的努力已失敗。

我該如何解決這個問題,以便UIPageViewController視圖控制器的滾動視圖被正確包含在容器ViewController的視圖內?

enter image description here

enter image description here

回答

2

我不得不以編程方式添加約束來解決這個問題。請注意,我不能使用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() 
} 
+1

您應該調用setNeedsLayout,而不是直接調用layoutSubviews。 – 2014-12-18 12:27:31

+0

我有一個奇怪的錯誤。 UIPageViewControllerContentView是UIPageViewController用來保存實際內容的類。 我將我的視圖以編程方式添加到頁面視圖控制器。 我在我的實際內容視圖和這個UIPageViewControllerContentView之間產生了一些不能令人滿意的約束,這導致我的滾動視圖內容被剪切。 self.pageViewController.view.setTranslatesAutoresizingMaskIntoConstraints(false) 修正了它。 – NameNotFoundException 2017-08-15 07:44:06

2

我有同樣的問題,並已通過添加以下代碼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) 

解決,我希望可以幫助別人。