objective-c - 向我添加到 UIWindow 的 View 添加约束时出错?

标签 objective-c autolayout uiwindow addsubview

抱歉我的英语很差。 我的代码如下:两个 View 将添加到窗口

   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 添加一些约束?是这个原因吗?或者其他原因

最佳答案

尝试在创建 View 后立即将 _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]];

关于objective-c - 向我添加到 UIWindow 的 View 添加约束时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30747889/

相关文章:

Ios 10 iPhone 7 键盘在收到通知时消失

ios - 将 UIWindow 声明为 let 而不是可选的 var 可以很好地编译。有什么陷阱吗?

ios - 在导航 Controller 中调用模态 Controller

ios Facebook登录-FBSDKAccessToken为零

ios - 自动布局 4 UILabels 垂直高度根据其内容/文本

ios - 滚动 UIScrollView 会改变它的 subview 框架

iphone - 如何在 UINavigationController 中的所有 View 前面显示一个 UIButton?

iOS 跨 View Controller 共享 'global data'

ios - 通过 Google plus 分享时出现错误 500

ios - 自动布局在自定义 TableViewCell 中被忽略