У меня есть два вида, создайте подпункты, создайте ограничение, затем примените его. MyView и Topview являются два UIViews под «» myOverylingViewОкончание приложения после применения ограничений программно
import UIKit
class ViewController: UIViewController {
@IBOutlet var myOverlyingView: UIView!
@IBOutlet weak var myView: UIView!
var x : Int = 500;
override func viewDidLoad() {
super.viewDidLoad()
var topview = NSKeyedUnarchiver.unarchiveObjectWithData(NSKeyedArchiver.archivedDataWithRootObject(self.myView)) as? UIView
myOverlyingView.addSubview(topview!)
topview!.backgroundColor = UIColor.blueColor()
var myConstraint : NSLayoutConstraint = NSLayoutConstraint(item: topview!, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.myView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
topview!.addConstraint(myConstraint)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
После применения ограничения я получаю ошибку завершения следующим образом.
2015-06-03 14:03:00.716 threadingTest1[30147:4645926] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fb43bb00500 UIView:0x7fb43bb00800.centerX == UIView:0x7fb43941ff30.centerX>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2015-06-03 14:03:00.720 threadingTest1[30147:4645926] View hierarchy unprepared for constraint.
Constraint: <NSLayoutConstraint:0x7fb43bb00500 UIView:0x7fb43bb00800.centerX == UIView:0x7fb43941ff30.centerX>
Container hierarchy:
<UIView: 0x7fb43bb00800; frame = (150 100; 300 65); autoresize = RM+BM; layer = <CALayer: 0x7fb43bb00000>>
View not found in container hierarchy: <UIView: 0x7fb43941ff30; frame = (150 100; 300 65); autoresize = RM+BM; layer = <CALayer: 0x7fb43941ef20>>
That view's superview: <UIView: 0x7fb43941fc20; frame = (0 0; 375 667); autoresize = RM+BM; layer = <CALayer: 0x7fb43941f260>>
2015-06-03 14:03:00.725 threadingTest1[30147:4645926] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint:<NSLayoutConstraint:0x7fb43bb00500 UIView:0x7fb43bb00800.centerX == UIView:0x7fb43941ff30.centerX> view:<UIView: 0x7fb43bb00800; frame = (150 100; 300 65); autoresize = RM+BM; layer = <CALayer: 0x7fb43bb00000>>'
*** First throw call stack:
(
0 CoreFoundation 0x0000000100c69c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001027d4bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000100c69b9d +[NSException raise:format:] + 205
3 Foundation 0x0000000101083479 -[NSLayoutConstraint _addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 187
4 UIKit 0x0000000101b83a34 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 474
5 Foundation 0x00000001010911be -[NSISEngine withBehaviors:performModifications:] + 155
6 UIKit 0x0000000101b8383a __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
7 UIKit 0x0000000101b8364d -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
8 UIKit 0x0000000101b83933 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 217
9 Foundation 0x00000001010911be -[NSISEngine withBehaviors:performModifications:] + 155
10 UIKit 0x0000000101b8383a __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
11 UIKit 0x0000000101b8364d -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
12 UIKit 0x0000000101b832de -[UIView(AdditionalLayoutSupport) _initializeHostedLayoutEngine] + 404
13 UIKit 0x0000000101b83de1 -[UIView(AdditionalLayoutSupport) _layoutEngineCreateIfNecessary] + 53
14 UIKit 0x0000000101b787e7 -[UIView(UIConstraintBasedLayout) _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 156
15 UIKit 0x0000000101b78b74 -[UIView(UIConstraintBasedLayout) _tryToAddConstraintWithoutUpdatingConstraintsArray:roundingAdjustment:mutuallyExclusiveConstraints:] + 30
16 UIKit 0x0000000101b78c9c -[UIView(UIConstraintBasedLayout) _tryToAddConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 243
17 threadingTest1 0x0000000100a6e625 _TFC14threadingTest114ViewController11viewDidLoadfS0_FT_T_ + 1637
18 threadingTest1 0x0000000100a6e6d2 _TToFC14threadingTest114ViewController11viewDidLoadfS0_FT_T_ + 34
19 UIKit 0x0000000101639210 -[UIViewController loadViewIfRequired] + 738
20 UIKit 0x000000010163940e -[UIViewController view] + 27
21 UIKit 0x00000001015542c9 -[UIWindow addRootViewControllerViewIfPossible] + 58
22 UIKit 0x000000010155468f -[UIWindow _setHidden:forced:] + 247
23 UIKit 0x0000000101560e21 -[UIWindow makeKeyAndVisible] + 42
24 UIKit 0x0000000101504457 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732
25 UIKit 0x00000001015071de -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
26 UIKit 0x00000001015060d5 -[UIApplication workspaceDidEndTransaction:] + 179
27 FrontBoardServices 0x00000001042e45e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
28 CoreFoundation 0x0000000100b9d41c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
29 CoreFoundation 0x0000000100b93165 __CFRunLoopDoBlocks + 341
30 CoreFoundation 0x0000000100b92f25 __CFRunLoopRun + 2389
31 CoreFoundation 0x0000000100b92366 CFRunLoopRunSpecific + 470
32 UIKit 0x0000000101505b42 -[UIApplication _run] + 413
33 UIKit 0x0000000101508900 UIApplicationMain + 1282
34 threadingTest1 0x0000000100a75057 main + 135
35 libdyld.dylib 0x0000000102f2c145 start + 1
36 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Это первая часть сообщения об ошибке выделяется для меня, как она спрашивает, если я пытаюсь сдерживать что-то вне поддерева зрения.
Я стараюсь следовать этому пониманию иерархии представлений здесь, Mac Developer Library, хотя я могу интерпретировать его неправильно.
Любая помощь будет оценена по достоинству.
Это не настоящий код. Пожалуйста, покажите свой реальный код. – matt