【问题标题】:iOS Autolayout rotation App crashiOS 自动布局旋转 App 崩溃
【发布时间】:2014-04-27 08:30:51
【问题描述】:
  1. 当我以纵向模式启动应用程序并将屏幕旋转到 风景,那么约束工作正常。

  2. 当我以纵向模式启动应用程序并展示模型时 视图控制器,我解散了模型视图控制器。然后我 旋转屏幕横向约束不起作用。

  3. 当我启动应用程序并旋转到横向模式时,当我 呈现模型视图控制器,应用程序崩溃,崩溃报告是 紧随其后,

模型视图控制器包含带有约束的导航栏(superview.trailing 和 navigationbar.trailing、superview.leading 和 navigationbar.leading 和 navigationbar.top 和 TopLayoutGuide.Bottom)。都是 0 常量。

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“自动布局不支持具有边缘布局约束的交叉旋转边界变换,例如右、左、上、下。有问题的视图是: >

步骤。创建一个新的基于单一视图的应用程序并添加一个名为 _backgroundImageView 的 imageView 和以下约束。

NSLayoutConstraint *backgroundImageConstraints1 = [NSLayoutConstraint constraintWithItem:_backgroundImageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];

NSLayoutConstraint *backgroundImageConstraints2 = [NSLayoutConstraint constraintWithItem:_backgroundImageView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];

NSLayoutConstraint *backgroundImageConstraints3 = [NSLayoutConstraint constraintWithItem:_backgroundImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0];

NSLayoutConstraint *backgroundImageConstraints4 = [NSLayoutConstraint constraintWithItem:_backgroundImageView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];

然后添加一个按钮,该按钮将作为 PresentModelView 带到第二个视图控制器。第二个视图仅包含 NavigationBar,它显示了下面的图像。

【问题讨论】:

  • 你有代码要显示吗?
  • 你真的添加了约束吗? [self.view addConstraints:backgroundImageConstraints1]; [self.view addConstraints:backgroundImageConstraints2]; [self.view addConstraints:backgroundImageConstraints3]; [self.view addConstraints:backgroundImageConstraints4];
  • 是的,我添加了.. [self.view addConstraints:@[backgroundImageConstraints1,backgroundImageConstraints2,backgroundImageConstraints3,backgroundImageConstraints4]];
  • 嗨,esker,我遇到了与 3 相同的问题。在我的情况下,我的应用程序固定为始终处于水平模式,但是当我以模态方式呈现视图控制器时,我得到了同样的错误。我像你一样使用自动布局。你找到解决方案了吗?谢谢

标签: ios objective-c crash rotation autolayout


【解决方案1】:

我通常不会以编程方式进行自动布局,但在你的情况下我会这样做。

  1. 为您的项目创建快照。
  2. 删除所有与自动布局相关的代码。
  3. 进入情节提要文件,选择要添加约束的 UI 元素后,单击“添加约束”按钮。

  1. 编辑弹出框中的信息,完成后点击“添加约束”。

对每个要对其使用约束的 UI 元素重复


通过这种方式重新进行自动布局,您将大大减少将错误引入代码中的机会,如您的问题所述。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 2015-06-05
    • 2019-11-29
    • 1970-01-01
    • 2014-03-10
    相关资源
    最近更新 更多