【问题标题】:UIPageViewController + Auto Layout rotation issueUIPageViewController + 自动布局旋转问题
【发布时间】:2013-07-18 16:40:59
【问题描述】:

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

【问题讨论】:

    标签: iphone ios rotation autolayout uipageviewcontroller


    【解决方案1】:

    我也有这个问题,以前的答案似乎没有多大帮助。我需要为与 PageViewController 一起使用的滚动视图添加约束。

    // 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)
    

    【讨论】:

    • 很好,我能够用这个答案在容器内使用 UIPageViewController 修复我的 AutoLayout 问题。
    • 谢谢!你是我的主人:)
    【解决方案2】:

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

    【讨论】:

    • 使用自动调整大小的蒙版可以解决问题,谢谢。我仍然不确定为什么包含的 UIViewController 的视图会受到其子视图的自动布局约束的影响。
    【解决方案3】:

    我想通了:

    我在容器控制器类中调用了[[_pageViewController view] setTranslatesAutoresizingMaskIntoConstraints:NO];,并没有在其视图(或Bored Astronaut 提到的视图)上显式设置自动布局约束。

    【讨论】:

    • 嗨,高级 - 我遇到了与您的问题中描述的完全相同的问题。您能否在此答案中提供一些示例代码?我正在通过可视格式语言(就像你一样)使用自动布局来做所有事情,当我旋转时,我的 UIPageViewController 完全按照你的方式做。您说“并且没有在其视图上明确设置自动布局约束”-如果您可以为此提供代码,它将极大地帮助我并提供更完整的答案。谢谢!
    【解决方案4】:

    @Bored Astronaut 的回答几乎对我有用。除了留下自动调整大小的掩码以转换为自动布局约束之外,我唯一做的就是设置UIPageViewController 的视图框架,然后再将其添加到父视图。

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

    如果不设置框架,子视图控制器将在任何方向正确显示和定位,但它们的子视图将无法正确布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多