以编程方式应用约束后应用终止

时间:2015-06-03 18:23:42

标签: ios swift uiview constraints nslayoutconstraint

我有两个视图,建立子视图,创建约束,然后应用它。 myView和topview是'myOverylingView'下的两个UIViews

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,但我可能会错误地解释它。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

这没有任何意义。你这样说:

@IBOutlet weak var myView: UIView!

但你也这样说:

myOverlyingView.addSubview(self.myView)

如果您希望myView作为 outlet 到达,则它必须位于接口中。那你怎么能把它作为子视图添加到任何东西?

如果作为出口到达,那么它是零,这就解释了为什么你不能设置它的约束。

无论哪种方式,这两行都是相互不连贯的。