向我添加到UIWindow的视图添加约束时出错?

时间:2015-06-10 05:37:39

标签: objective-c autolayout uiwindow addsubview

对不起,我的英语很差。 我的代码如下:两个视图将添加到窗口

   UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    if (!_transparentView) {
        _transparentView  = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_BOUNDS.size.width, SCREEN_BOUNDS.size.height)];
        [_transparentView setBackgroundColor:[UIColor blackColor]];
        [_transparentView setAlpha:0.5];
        [window addSubview:_transparentView];
    }
    [_transparentView setHidden:NO];
     _noSupportairDropTipView = [[[NSBundle mainBundle] loadNibNamed:@"ConnectPhoneTipView" owner:self options:nil] lastObject];

 [window addConstraint:[NSLayoutConstraint constraintWithItem:_noSupportairDropTipView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:window attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
[window addConstraint:[NSLayoutConstraint constraintWithItem:_noSupportairDropTipView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:window attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
 [_noSupportairDropTipView addConstraint:[NSLayoutConstraint constraintWithItem:_noSupportairDropTipView attribute:NSLayoutAttributeWidth relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:293]];
 [_noSupportairDropTipView addConstraint:[NSLayoutConstraint constraintWithItem:_noSupportairDropTipView attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:277]];

 [window addSubview:_noSupportairDropTipView];

。我得到了如下的错误。我不知道为什么。

 <UIView: 0x190c3e10; frame = (0 0; 320 480); alpha = 0.5; layer =
<CALayer: 0x191b45f0>>  View not found in container hierarchy:
<ConnectPhoneTipView: 0x190d8920; frame = (0 0; 293 277); autoresize
= RM+BM; layer = <CALayer: 0x1917b5e0>>     That view's superview: NO SUPERVIEW libc++abi.dylib: terminate_handler unexpectedly threw an
exception

我是否必须为_transparentView添加一些约束?是这个原因?或其他原因

1 个答案:

答案 0 :(得分:2)

尝试在创建视图后立即向窗口添加_noSupportairDropTipView,然后添加约束:

 _noSupportairDropTipView = [[[NSBundle mainBundle] loadNibNamed:@"ConnectPhoneTipView" owner:self options:nil] lastObject];
[window addSubview:_noSupportairDropTipView];

[window addConstraint:[NSLayoutConstraint constraintWithItem:_noSupportairDropTipView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:window attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
[window addConstraint:[NSLayoutConstraint constraintWithItem:_noSupportairDropTipView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:window attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
[_noSupportairDropTipView addConstraint:[NSLayoutConstraint constraintWithItem:_noSupportairDropTipView attribute:NSLayoutAttributeWidth relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:293]];
[_noSupportairDropTipView addConstraint:[NSLayoutConstraint constraintWithItem:_noSupportairDropTipView attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:277]];