NSGenericException', reason: 'Unable to install constraint on view

后端 未结 7 1508
花落未央
花落未央 2020-12-09 16:05

Terminating app due to uncaught exception \'NSGenericException\'

Terminating app due to uncaught exception \'NSGenericException\', reason: \'Unable to

相关标签:
7条回答
  • 2020-12-09 16:12

    Same error, different solution here:

    I got this error on starting up my app on iOS 6 after adding a new view and forgetting to switch off Use Auto Layout on it in the interface builder ... I hate it there's no standard setting to NOT use auto layout by default for new views ...

    0 讨论(0)
  • 2020-12-09 16:14

    I found adding this one line of code fixed this issue for a cocoa ScrollView.

    [scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
    

    I think certain views add constraints at run time therefore conflicting when you add your own via objective c, so you need to disable this behaviour...

    0 讨论(0)
  • 2020-12-09 16:16

    Similar to neoneye I was getting this due to removing subviews with constraints. However I had a constraint that was positioning the parent view, and this was being removed if I called [self.view removeConstraints:self.view.constraints]; Instead I made this change,

    Original Code:

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

    Fixed to remove constraints on subviews:

    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];
    }
    

    UPDATE: So I hit this error again - and it was due to removing a single view this time. Added a function to remove the view cleanly:

    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];
    }
    
    0 讨论(0)
  • 2020-12-09 16:18

    You need to install the constraint on the "higher" of the two views. A good, general way to do this is like this:

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

    Just a word of caution: It's good to remember here that constraint attributes are evaluated in the context of the view on which they are added. So for instance, the value of NSLayoutAttributeLeft of viewA, for a constraint installed on viewB, is interpreted in the coordinate space of viewB. For constraints that only reference sibling views or their superview, that fact is largely irrelevant, but there's no restriction that constraints can't reference two views that aren't siblings or direct parents.

    0 讨论(0)
  • 2020-12-09 16:25

    I experienced this error on iOS6. In my case it was because I started removing subviews without first removing constraints.

    // 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];
    
    0 讨论(0)
  • 2020-12-09 16:34

    I got this problem using a UIPickerView like input of a UITextField (using Autolayout). When I push another viewController and thus a pop it to the viewController with the picker, the app crashes. I found the following solution, in the UIPickerViewController:

    -(void)viewWillAppear:(BOOL)animated{
    
        [self.pickerView removeFromSuperview];
        [self.pickerView setTranslateAutoresizingMaskIntoContraints:YES];
        [self.view addSubview];
    
    }   
    

    You can also set the UIPickerViewPosition after removing from superview. I hope that can help you!

    0 讨论(0)
提交回复
热议问题