2013-07-18 115 views
12

我有一個簡單的UIViewController裏面的UIPageViewController。UIPageViewController +自動佈局旋轉問題

如果UIViewController沒有子視圖,它的視圖在旋轉時正確調整大小。 (純綠色背景)。

portraitlandscape

如果所述的UIViewController具有帶有固定幀中的子視圖中,當旋轉時其視圖正確調整大小。 (帶角落的黃色正方形的純綠色背景)。

portraitlandscape

如果在UIViewController中有設置,以填補它的父它的自動佈局約束子視圖,旋轉時,其視圖不再正確調整大小。 (帶有UIPageViewController紅色背景的純黃色背景可見)。我使用的自動佈局代碼:

UIView *v = [[UIView alloc] init]; 
[v setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[v setBackgroundColor:[UIColor yellowColor]]; 
[[self view] addSubview:v]; 

NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(v); 

NSArray *cs = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]|" options:0 metrics:nil views:viewsDictionary]; 
[[self view] addConstraints:cs]; 

cs = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[v]|" options:0 metrics:nil views:viewsDictionary]; 
[[self view] addConstraints:cs]; 

portraitlandscape

爲什麼一個子視圖的自動佈局影響它的父?只有當它包含在UIPageViewController中時纔會發生這種情況。

回答

0

我計算出來:

我曾經在容器控制器類稱爲[[_pageViewController view] setTranslatesAutoresizingMaskIntoConstraints:NO];而不是顯式地設置在它的視圖自動佈局約束(或視圖,如鑽孔宇航員提到的)。

+2

嗨,高級 - 我有你的問題中描述的完全相同的問題。你能用這個答案提供一些示例代碼嗎?我正在通過Visual Format Language(和你一樣)自動佈局,當我旋轉時,我的UIPageViewController完全按照你的做法做。你說「而不是明確地設置其視圖上的自動佈局約束」 - 如果你可以提供代碼,它會幫助我很大,並提供更完整的答案。謝謝! – DiscDev

7

頁面視圖控制器的視圖不是代表頁面視圖的超級視圖。頁面視圖控制器的視圖具有由自定義滾動視圖子類組成的視圖層次結構,並且該滾動視圖又具有三個通用視圖子類,每個視圖子類用於索引0至2處的包含視圖控制器的視圖。這些用作你的看法。您必須將自動佈局約束定位到這些視圖。但使用自動調整掩碼並讓它們轉換爲自動佈局約束要容易得多。

+0

使用自動調整掩碼修復了這個問題,謝謝。我仍然不確定爲什麼包含的UIViewController的視圖會受到其子視圖的自動佈局約束的影響。 – Senior

10

我也有這個問題,以前的答案似乎沒有太大的幫助。我需要爲與PageViewController一起使用的scrollview添加約束。

// Add Paging View Controller 
self.addChildViewController(self.pageViewController) 
self.pageViewController.didMoveToParentViewController(self) 
self.containerView.addSubview(self.pageViewController.view) 

// Add Constraints 
var verticalConstraints:Array = NSLayoutConstraint.constraintsWithVisualFormat("V:|[View]|", options: nil, metrics: nil, views: ["View":self.pageViewController.view]) 
var horizontalConstraints:Array = NSLayoutConstraint.constraintsWithVisualFormat("|[View]|", options: nil, metrics: nil, views: ["View":self.pageViewController.view]) 

self.pageViewController.view.setTranslatesAutoresizingMaskIntoConstraints(false) 
self.containerView.addConstraints(verticalConstraints) 
self.containerView.addConstraints(horizontalConstraints) 

// Add Constraints for the Scrollview 
//This line is a bit of a hack. If Apple ever changes the structure of the UIPageViewController, this could break. 
let scrollView = self.pageViewController.view.subviews.first! as UIView 
scrollView.setTranslatesAutoresizingMaskIntoConstraints(false) 
verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[View]|", options: nil, metrics: nil, views: ["View":scrollView]) 
horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("|[View]|", options: nil, metrics: nil, views: ["View":scrollView]) 
self.pageViewController.view.addConstraints(verticalConstraints) 
self.pageViewController.view.addConstraints(horizontalConstraints) 
+0

尼斯之一,我能夠與UIPageViewController解決這個答案的容器內自動版式我的問題。 – Paludis

+0

謝謝!你是我的主人:) – Unmerciful

0

@Bored宇航員的答案几乎爲我工作。除了將自動調整掩碼轉換爲自動佈局約束之外,我做的唯一事情是在將其添加到父視圖之前設置UIPageViewController的視圖框架。

[self addChildViewController:pageViewController]; 
pageViewController.view.frame = self.view.frame; 
[self.view addSubview:pageViewController.view]; 
[pageViewController didMoveToParentViewController:self]; 

沒有設置框架,子視圖控制器將顯示並正確定位在任何方向,但其子視圖將不會正確佈局。