iphone - UIPageViewController + 自动布局旋转问题

标签 iphone ios rotation autolayout uipageviewcontroller

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

如果 UIViewController 没有 subview ,它的 View 在旋转时会正确调整大小。 (纯绿色背景)。

portrait landscape

如果 UIViewController 有一个带有固定框架的 subview ,它的 View 在旋转时会正确调整大小。 (纯绿色背景,角落有黄色方 block )。

portrait landscape

如果 UIViewController 有一个 subview ,其自动布局约束设置为填充其父 View ,则其 View 在旋转时不再正确调整大小。 (纯黄色背景,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];

portrait landscape

为什么 subview 的自动布局会影响它的父 View ?只有当它包含在 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)

关于iphone - UIPageViewController + 自动布局旋转问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17729336/

相关文章:

ios - 计算两个 CLLocationCoordinate2D 之间的方位

ios - 在 Swift 中获取 Google map 中 Lat Long 的地点 ID

iOS:UITableView 单元格在滚动时更改选择状态

javascript - Jquery CSS 背景图像旋转

OpenGL 围绕一个点旋转相机

iphone - NSInteger 和 facebookId

ios - 当我减少我的应用程序的销售地区数量时,谁会收到更新?

iphone - 使用四元数代替滚动、俯仰和偏航来跟踪设备运动

ios - 模态视图关闭时如何获得准确的 PresentingViewController

ios - 在旋转时调整 UIViewController 内的全屏 View 大小?