error when adding constraints to a view I added to UIWindow?

1.4k views Asked by At

I'm sorry my english is poor. My code like below: two views will be added to the window

   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];

. i got error like below.i don't know why.

 <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

Do i have to add some constraints to _transparentView? is that reason? or other reason

1

There are 1 answers

2
uraimo On BEST ANSWER

Try adding _noSupportairDropTipView to window right after the view is created, and add constraints after that:

 _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]];