UIPageViewController +自动布局旋转问题

我有一个UIPageViewController,里面有一个简单的UIViewController。

如果UIViewController没有子视图,则它的视图在旋转时正确调整大小。 (纯绿色背景)。

如果UIViewController具有固定框架的子视图,则旋转时其视图将正确调整大小。 (带角落的黄色正方形的纯绿色背景)。

如果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];

为什么子视图的自动布局会影响其超视图? 只有当它包含在UIPageViewController中时才会发生这种情况。


我也有这个问题,以前的答案似乎没有多大帮助。 我需要为与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至2处的包含视图控制器的视图。这些用作你的看法。 您必须将自动布局约束定位到这些视图。 但使用自动调整掩码并让它们转换为自动布局约束要容易得多。


我想到了:

我已经调用[[_pageViewController view] setTranslatesAutoresizingMaskIntoConstraints:NO]; 在容器控制器类中,并没有明确地设置其视图(或视图,如钻孔宇航员提到的)的自动布局约束。

链接地址: http://www.djcxy.com/p/28285.html

上一篇: UIPageViewController + Auto Layout rotation issue

下一篇: Dynamic UIView height with auto layout in iOS 6