以编程方式向IB场景添加约束

时间:2015-03-13 11:10:29

标签: ios swift uiview uiviewcontroller interface-builder

我试图为场景添加子视图和一些约束,这是使用界面构建器构建的。
子视图被正确添加,但是当我尝试添加约束时,应用程序崩溃时出现以下错误,这意味着我已将视图层次结构混淆了。但是,我无法找到我的错误。

Shopigator[14377:635114] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fafab4bb660 THSegmentedControl:0x7fafaf03a6e0.centerX == UISearchBar:0x7fafaf0334b0.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-03-13 11:51:12.004 Shopigator[14377:635114] View hierarchy unprepared for constraint.
Constraint: <NSLayoutConstraint:0x7fafab4bb660 THSegmentedControl:0x7fafaf03a6e0.centerX == UISearchBar:0x7fafaf0334b0.centerX>
Container hierarchy: 
<UIView: 0x7fafaf037d70; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x7fafab424a00>>
| <_UILayoutGuide: 0x7fafaf037e40; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x7fafa97e4710>>
| <_UILayoutGuide: 0x7fafaf037540; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x7fafaf0ed980>>
| <UIView: 0x7fafaf035e40; frame = (0 0; 500 50); layer = <CALayer: 0x7fafaf0a9be0>>
| <THSegmentedControl: 0x7fafaf03a6e0; baseClass = UIControl; frame = (34 297; 332 29); clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7fafaf0398d0>>
View not found in container hierarchy: <UISearchBar: 0x7fafaf0334b0; frame = (0 0; 320 44); text = ''; opaque = NO; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x7fafb10ad1f0>; layer = <CALayer: 0x7fafab44ed10>>
That view's superview: NO SUPERVIEW
2015-03-13 11:51:12.060 Shopigator[14377:635114] *** 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:0x7fafab4bb660 THSegmentedControl:0x7fafaf03a6e0.centerX == UISearchBar:0x7fafaf0334b0.centerX> view:<UIView: 0x7fafaf037d70; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x7fafab424a00>>'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000108a2df35 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x00000001086c6bb7 objc_exception_throw + 45
2   CoreFoundation                      0x0000000108a2de6d +[NSException raise:format:] + 205
3   Foundation                          0x0000000107f6e0f9 -[NSLayoutConstraint _addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 187
4   UIKit                               0x0000000109b77de5 __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke_2 + 474
5   Foundation                          0x0000000107f7bd6e -[NSISEngine withBehaviors:performModifications:] + 155
6   UIKit                               0x0000000109b77beb __57-[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:]_block_invoke + 452
7   UIKit                               0x0000000109b779fe -[UIView(AdditionalLayoutSupport) _switchToLayoutEngine:] + 197
8   UIKit                               0x0000000109b77671 -[UIView(AdditionalLayoutSupport) _initializeHostedLayoutEngine] + 404
9   UIKit                               0x0000000109b78191 -[UIView(AdditionalLayoutSupport) _layoutEngineCreateIfNecessary] + 53
10  UIKit                               0x0000000109b6cb0a -[UIView(UIConstraintBasedLayout) _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 156
11  UIKit                               0x0000000109b6cea4 -[UIView(UIConstraintBasedLayout) _tryToAddConstraintWithoutUpdatingConstraintsArray:roundingAdjustment:mutuallyExclusiveConstraints:] + 30
12  UIKit                               0x0000000109b6cfcc -[UIView(UIConstraintBasedLayout) _tryToAddConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 243
13  Shopigator                          0x0000000107507617 _TFC10Shopigator20FilterViewController11viewDidLoadfS0_FT_T_ + 9623
14  Shopigator                          0x0000000107508d92 _TToFC10Shopigator20FilterViewController11viewDidLoadfS0_FT_T_ + 34
15  UIKit                               0x0000000109617a90 -[UIViewController loadViewIfRequired] + 738
16  UIKit                               0x0000000109617c8e -[UIViewController view] + 27
17  UIKit                               0x0000000109bb941e -[_UIFullscreenPresentationController _setPresentedViewController:] + 65
18  UIKit                               0x00000001095f3429 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 105
19  UIKit                               0x0000000109623a41 -[UIViewController _presentViewController:withAnimationController:completion:] + 1746
20  UIKit                               0x0000000109625d81 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 132
21  UIKit                               0x0000000109625ca5 -[UIViewController presentViewController:animated:completion:] + 229
22  UIKit                               0x00000001094f38be -[UIApplication sendAction:to:from:forEvent:] + 75
23  UIKit                               0x00000001095fa410 -[UIControl _sendActionsForEvents:withEvent:] + 467
24  UIKit                               0x00000001095f97df -[UIControl touchesEnded:withEvent:] + 522
25  UIKit                               0x0000000109539308 -[UIWindow _sendTouchesForEvent:] + 735
26  UIKit                               0x0000000109539c33 -[UIWindow sendEvent:] + 683
27  UIKit                               0x00000001095069b1 -[UIApplication sendEvent:] + 246
28  UIKit                               0x0000000109513a7d _UIApplicationHandleEventFromQueueEvent + 17370
29  UIKit                               0x00000001094ef103 _UIApplicationHandleEventQueue + 1961
30  CoreFoundation                      0x0000000108963551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
31  CoreFoundation                      0x000000010895941d __CFRunLoopDoSources0 + 269
32  CoreFoundation                      0x0000000108958a54 __CFRunLoopRun + 868
33  CoreFoundation                      0x0000000108958486 CFRunLoopRunSpecific + 470
34  GraphicsServices                    0x000000010b2849f0 GSEventRunModal + 161
35  UIKit                               0x00000001094f2420 UIApplicationMain + 1282
36  Shopigator                          0x000000010748081e top_level_code + 78
37  Shopigator                          0x000000010748085a main + 42
38  libdyld.dylib                       0x000000010b7a8145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我的ViewController的相关部分

@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var radiusSlider: LoagarithmicSlider!
var priceClassSelector: THSegmentedControl?

覆盖func viewDidLoad(){         super.viewDidLoad()

    var pCFrame = CGRectMake(34, 297, 332, 29)
    self.priceClassSelector = THSegmentedControl(segments: SearchFilter.PRICE_CLASS_LABELS)
    self.priceClassSelector!.frame = pCFrame
    self.priceClassSelector?.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(self.priceClassSelector!)

    self.view.addConstraint(NSLayoutConstraint(
        item: self.priceClassSelector!,
        attribute: NSLayoutAttribute.CenterX,
        relatedBy: NSLayoutRelation.Equal,
        toItem: self.searchBar,
        attribute: NSLayoutAttribute.CenterX,
        multiplier: 1.0,
        constant: 0))
}

IB中的视图层次结构

view hierarchy in IB

0 个答案:

没有答案
相关问题