NSGenericException',原因:'无法在视图上安装约束

问题描述 投票:24回答:7

因未捕获的异常'NSGenericException'而终止应用程序

由于未捕获的异常'NSGenericException'而终止应用程序,原因:'无法在视图上安装约束。约束是否引用了视图子树之外的内容?那是违法的。约束:view:; layer =; contentOffset:{0,0}>'

iphone objective-c ios6 xcode4.5
7个回答
42
投票

您需要在两个视图的“更高”上安装约束。一个好的,通用的方法是这样的:

NSLayoutConstraint* constraint = ...;
NSView* firstView = constraint.firstItem;
NSView* secondView = constraint.secondItem;    
[[firstView ancestorSharedWithView: secondView] addConstraint: constraint];

请注意:这里要记住,约束属性是在添加它们的视图的上下文中进行评估的。因此,例如,对于viewB上安装的约束,viewA的NSLayoutAttributeLeft的值在viewB的坐标空间中进行解释。对于仅引用同级视图或其超级视图的约束,该事实在很大程度上是不相关的,但是约束不能引用两个不是兄弟或直接父母的视图。


10
投票

与neoneye类似,由于删除了带有约束的子视图,我得到了这个。但是我有一个约束定位父视图,如果我调用[self.view removeConstraints:self.view.constraints];,这个被删除而是我做了这个改变,

原始代码:

for (UIView *subview in [view subviews]) {
    [subview removeFromSuperview];
}

已修复以删除子视图的约束:

NSMutableArray * constraints_to_remove = [ @[] mutableCopy] ;
for( NSLayoutConstraint * constraint in view.constraints) {
    if( [view.subviews containsObject:constraint.firstItem] ||
       [view.subviews containsObject:constraint.secondItem] ) {
        [constraints_to_remove addObject:constraint];
    }
}
[view removeConstraints:constraints_to_remove];

for (UIView *subview in [view subviews]) {
    [subview removeFromSuperview];
}

更新:所以我再次遇到这个错误 - 这是因为这次删除了一个视图。添加了一个干净地删除视图的功能:

void cleanRemoveFromSuperview( UIView * view ) {
  if(!view || !view.superview) return;

  //First remove any constraints on the superview
  NSMutableArray * constraints_to_remove = [NSMutableArray new];
  UIView * superview = view.superview;

  for( NSLayoutConstraint * constraint in superview.constraints) {
    if( constraint.firstItem == view ||constraint.secondItem == view ) {
      [constraints_to_remove addObject:constraint];
    }
  }
  [superview removeConstraints:constraints_to_remove];

  //Then remove the view itself.
  [view removeFromSuperview];
}

3
投票

我在iOS6上遇到过这个错误。在我的情况下,这是因为我开始删除子视图而没有先删除约束。

// I had forgotten to remove constraints first. This caused the crash.
[self.view removeConstraints:self.view.constraints];

NSArray *subviews = self.view.subviews;
for (UIView *subview in subviews) {
    [subview removeFromSuperview];
}

[self addYourSubviewsHere];

2
投票

我使用UIPickerView输入UITextField(使用Autolayout)时遇到了这个问题。当我推送另一个viewController并因此使用选择器将其弹出到viewController时,应用程序崩溃了。我在UIPickerViewController中找到了以下解决方案:

-(void)viewWillAppear:(BOOL)animated{

    [self.pickerView removeFromSuperview];
    [self.pickerView setTranslateAutoresizingMaskIntoContraints:YES];
    [self.view addSubview];

}   

您还可以在从superview中删除后设置UIPickerViewPosition。我希望能帮到你!


0
投票

我发现添加这一行代码修复了这个问题的可可ScrollView。

[scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];

我认为某些视图会在运行时添加约束,因此当您添加自己的目标c时会发生冲突,因此您需要禁用此行为...


0
投票

相同的错误,不同的解决方案:

在添加新视图并忘记在界面构建器中关闭Use Auto Layout后,我在iOS 6上启动我的应用程序时出现此错误...我讨厌它没有标准设置默认情况下不使用自动布局新视图。 ..


0
投票

我遇到了同样的崩溃,结果是带有约束乘数值的浮点精度问题。我将所有约束乘数转换为漂亮的浮点值(例如0.375而不是0.35)并修复了崩溃。

AutoLayout: removeFromSuperview / removeConstraints throws exception and crashes hard

© www.soinside.com 2019 - 2024. All rights reserved.