UIPageViewController +自动布局旋转问题

问题描述:

我有一个简单的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中时才会发生这种情况。

我计算出来:

我曾经在容器控制器类称为[[_pageViewController view] setTranslatesAutoresizingMaskIntoConstraints:NO];而不是显式地设置在它的视图自动布局约束(或视图,如钻孔宇航员提到的)。

+2

嗨,高级 - 我有你的问题中描述的完全相同的问题。你能用这个答案提供一些示例代码吗?我正在通过Visual Format Language(和你一样)自动布局,当我旋转时,我的UIPageViewController完全按照你的做法做。你说“而不是明确地设置其视图上的自动布局约束” - 如果你可以提供代码,它会帮助我很大,并提供更完整的答案。谢谢! – DiscDev

页面视图控制器的视图不是代表页面视图的超级视图。页面视图控制器的视图具有由自定义滚动视图子类组成的视图层次结构,并且该滚动视图又具有三个通用视图子类,每个视图子类用于索引0至2处的包含视图控制器的视图。这些用作你的看法。您必须将自动布局约束定位到这些视图。但使用自动调整掩码并让它们转换为自动布局约束要容易得多。

+0

使用自动调整掩码修复了这个问题,谢谢。我仍然不确定为什么包含的UIViewController的视图会受到其子视图的自动布局约束的影响。 – Senior

我也有这个问题,以前的答案似乎没有太大的帮助。我需要为与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

@Bored宇航员的答案几乎为我工作。除了将自动调整掩码转换为自动布局约束之外,我做的唯一事情是在将其添加到父视图之前设置UIPageViewController的视图框架。

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

没有设置框架,子视图控制器将显示并正确定位在任何方向,但其子视图将不会正确布局。