美好的一天,朋友们!
我没有尝试动画从代码中设置在IB中的视图.应用崩溃的原因如下:
The view hierarchy is not prepared for the constraint…
我在这里看到了一些类似的问题,原因始终是以编程方式创建的视图尚未添加到superview中.但我在IB中创建了所有观点!
控制台还说:
View not found in container hierarchy: (here goes it’s superview)
它对我没有任何意义,因为事实上它是适当的superview的子视图,xcode知道它 – 它立即打印视图层次结构,它适合.
可能是什么原因?
谢谢!
编辑:我使用的代码:
- (void)setEditingConstraintsForView:(UIView *)view { // Pin given view to top,fix it height NSDictionary *givenView = @{@"view":view}; view.translatesAutoresizingMaskIntoConstraints = NO; NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:nil views:givenView]; NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view(height)]" options:0 metrics:@{@"height":@(viewHeight)} views:givenView]; [self.animatedVIew addSubview:view]; for (NSArray *constraints in @[horizontalConstraints,verticalConstraints]) { [view addConstraints:constraints]; } }
我还删除了在安装new之前在IB中设置的所有约束:
- (NSDictionary *)constraintsFromIB { if (!_constraintsFromIB) { _constraintsFromIB = @{@"view1":self.view1.referencingConstraintsInSuperviews,@"view2":self.view2.referencingConstraintsInSuperviews,@"view3":self.view3.referencingConstraintsInSuperviews }; } return _constraintsFromIB; }
然后:
- (void)updateViewConstraints { [super updateViewConstraints]; // clear all constraints for (NSString *viewName in self.constraintsFromIB.allKeys) { for (NSLayoutConstraint *constraint in self.constraintsFromIB[viewName]) { [constraint remove]; }
}
}
- (void)animateConstraintsForState:(LDYEditingLabel)state { self.editingLabel = state; [UIView animateWithDuration:0.3 animations:^{ [self updateViewConstraints]; [self.view layoutIfNeeded]; }]; }
稍后在updateViewConstraints中:有一个代码触发我的方法setEditingConstraintsForView:(UIView *)视图