向我添加到 UIWindow 的视图添加约束时出错?
Posted
技术标签:
【中文标题】向我添加到 UIWindow 的视图添加约束时出错?【英文标题】:error when adding constraints to a view I added to UIWindow? 【发布时间】:2015-06-10 05:37:38 【问题描述】:对不起,我的英语很差。 我的代码如下:两个视图将添加到窗口中
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 添加一些约束?是这个原因吗?或其他原因
【问题讨论】:
ConnectPhoneTipView
在哪里?该错误与此有关,但我在您的代码 sn-p 中没有看到它。
_supportAirDropTipView = [[[NSBundle mainBundle] loadNibNamed:@"ConnectPhoneTipView" owner:self options:nil] lastObject];
您正在添加_noSupportairDropTipView
,但没有使用_SupportairDropTipView
(没有no
)这是正确的吗?还有,_noSupportairDropTipView
是从哪里来的?
我很粗心。我更新了问题。请告诉我为什么会崩溃
【参考方案1】:
尝试在视图创建后立即将_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]];
【讨论】:
太棒了!你是对的!现在它显示出来了。但是现在我想问你一个问题。如果uiview的子视图变大,如何让uiview变大。 我知道该怎么做了。非常感谢。以上是关于向我添加到 UIWindow 的视图添加约束时出错?的主要内容,如果未能解决你的问题,请参考以下文章
如果我将它作为子视图添加到我的 UIPickerView,是不是需要向我的 UIToolBar 添加约束?
将 UIView 添加到 UIWindow 的子视图时未检测到画外音