这是我的代码:
int x= 17;
int y = 27;
int width = 287;
int height = 181;
int mainview_height = 568;
int x_spacer = 0;
int y_spacer = 0;
int width_spacer = 320;
int height_spacer = 27;
UIView *view_spacer = [[UIView alloc]initWithFrame:CGRectMake(x_spacer, y_spacer, width_spacer, height_spacer)];
[view_spacer setBackgroundColor:[UIColor clearColor]];
[view_spacer setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.mainView addSubview:view_spacer];
UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
[view1 setBackgroundColor:[UIColor whiteColor]];
[view1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.mainView addSubview:view1];
[view_spacer addConstraint:[NSLayoutConstraint constraintWithItem:view_spacer attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual toItem:view_spacer attribute:NSLayoutAttributeHeight multiplier:width_spacer/height_spacer constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view_spacer attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeTop multiplier:1 constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view_spacer attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeHeight multiplier:width_spacer/mainview_height constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:self.mainView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view_spacer attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
[view_spacer addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view_spacer attribute:NSLayoutAttributeBottom multiplier:1 constant:0.0f]];
[view1 addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeHeight
multiplier:width/height constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual toItem:self.mainView attribute:NSLayoutAttributeHeight multiplier:width/mainview_height constant:0.0f]];
[self.mainView addConstraint:[NSLayoutConstraint constraintWithItem:self.mainView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
更新自动布局约束时出现以下错误
未为约束准备视图层次结构: 添加到。时 在视图中,约束的项必须是该视图的后代(或 视图本身)。如果需要约束,这将崩溃 在组装视图层次结构之前解析。打破 - [UIView _viewHierarchyUnpreparedForConstraint:]进行调试。 2015-07-18 12:54:02.469 LandCorp Cleaning App [2022:64687]查看层次结构 没有准备好限制。约束: 容器 层次结构:>在容器层次结构中找不到视图: >该视图的超级视图:> 2015-07-18 12:54:02.472 LandCorp Cleaning 应用程序[2022:64687] *由于未捕获的异常而终止应用程序 'NSGenericException',原因:'无法在视图上安装约束。 约束是否引用了子树之外的内容 风景?那是违法的。 约束:查看:>' * 第一次抛出调用堆栈:(0 CoreFoundation 0x000000010799dc65 exceptionPreprocess + 165 1 libobjc.A.dylib
0x0000000107636bb7 objc_exception_throw + 45 2 CoreFoundation
0x000000010799db9d + [NSException raise:format:] + 205 3 Foundation 0x00000001071c8479 - [NSLayoutConstraint _addToEngine:integralizationAdjustment:mutualExclusiveConstraints:] + 187 4 UIKit 0x0000000108406786 - [UIView(UIConstraintBasedLayout)_layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutualExclusiveConstraints:] + 411 5 UIKit 0x0000000108406a14 - [UIView(UIConstraintBasedLayout)_tryToAddConstraintWithoutUpdatingConstraintsArray:roundingAdjustment:mutualExclusiveConstraints:] + 30 6 UIKit 0x0000000108406b3c - [UIView(UIConstraintBasedLayout)_tryToAddConstraint:roundingAdjustment:mutualExclusiveConstraints:] + 243 7 LandCorp Cleaning App 0x00000001070e7e47 - [InspectionDetails populateDataViews] + 2007 8 LandCorp Cleaning App 0x00000001070e748c - [InspectionDetails connection:didReceiveData:] + 1404 9 CFNetwork
0x00000001097dbd6c __65- [NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] _ block_invoke + 69 10 CFNetwork 0x00000001097dbd10 - [NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 199 11 CFNetwork 0x00000001097dbe77 - [NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 48 12 CFNetwork 0x00000001097c6089 _ZL30_NSURLConnectionDidReceiveDataP16_CFURLConnectionPK8__CFDatalPKv + 79 13 CFNetwork 0x00000001096a85e0 ___ZN27URLConnectionClient_Classic29_delegate_didReceiveDataArrayEv_block_invoke + 302 14 CFNetwork 0x0000000109778a71 ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 273 15 CFNetwork 0x00000001096965d6 _ZN19RunloopBlockContext13_invoke_blockEPKvPv + 72 16 CoreFoundation 0x00000001078a4354 CFArrayApplyFunction + 68 17 CFNetwork
0x0000000109696497 _ZN19RunloopBlockContext7performEv + 133 18 CFNetwork 0x00000001096962d6 _ZN17MultiplexerSource7performEv + 256 19 CFNetwork 0x00000001096960ec _ZN17MultiplexerSource8_performEPv + 72 20 CoreFoundation 0x00000001078d1431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 21 CoreFoundation 0x00000001078c72fd __CFRunLoopDoSources0 + 269 22 CoreFoundation 0x00000001078c6934 __CFRunLoopRun + 868 23 CoreFoundation
0x00000001078c6366 CFRunLoopRunSpecific + 470 24图形服务
0x000000010aec3a3e GSEventRunModal + 161 25 UIKit
0x0000000107d968c0 UIApplicationMain + 1282 26 LandCorp清洁应用程序 0x00000001070e897f main + 111 27 libdyld.dylib
0x0000000109f41145 start + 1)libc ++ abi.dylib:终止于 NSException类型的未捕获异常