Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 向UIWindow添加子视图时的自动布局_Objective C_Uialertview_Autolayout_Uiwindow_Nslayoutconstraint - Fatal编程技术网

Objective c 向UIWindow添加子视图时的自动布局

Objective c 向UIWindow添加子视图时的自动布局,objective-c,uialertview,autolayout,uiwindow,nslayoutconstraint,Objective C,Uialertview,Autolayout,Uiwindow,Nslayoutconstraint,我正在创建需要作为子视图添加到UIWindow的customalertview。我已经为它的子视图正确地设置了约束,但是我不知道何时/如何设置视图本身相对于窗口的约束 我希望alertview的宽度为屏幕宽度的70%。高度已与其子视图相关 -(无效)显示{ UIWindow*window=[[UIApplication sharedApplication]keyWindow]; [窗口添加子视图:self]; [self-addConstraint:[NSLayoutConstraint Con

我正在创建需要作为子视图添加到UIWindow的customalertview。我已经为它的子视图正确地设置了约束,但是我不知道何时/如何设置视图本身相对于窗口的约束

我希望alertview的宽度为屏幕宽度的70%。高度已与其子视图相关

-(无效)显示{
UIWindow*window=[[UIApplication sharedApplication]keyWindow];
[窗口添加子视图:self];
[self-addConstraint:[NSLayoutConstraint ConstraintWidth:self属性:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:[UIApplication sharedApplication]。keyWindow属性:NSLayoutAttributeWidth乘数:0.7常量:0];
[self-addConstraint:[NSLayoutConstraint constraintWithItem:self属性:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual-toItem:_-messageLabel属性:NSLayoutAttributeBottom乘数:1.0常量:10];
[self-addConstraint:[NSLayoutConstraint constraintWithItem:self属性:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:(\u imageView.image?\u imageView:\u titleLabel)属性:NSLayoutAttributeTop乘数:1.0常量:-10];
}
我做错了什么?我收到以下错误消息:

NSLayoutConstraint:0x181cd7c0 WFSoftAlertView:0x16e384c0.width == 0.7*UIWindow:0x16e7c8c0.width>
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.

尝试将您的constraintWithItem从 自己 到
self.view

尝试将您的constraintWithItem从
WFSoftAlertView.translatesAutoresizingMaskIntoConstraints=NO;
NSDictionary *views = NSDictionaryOfVariableBindings(WFSoftAlertView);
float width30 = self.view.frame.size.width*.3;
width30 = width30/2; // divided by 2 for padding of its left/rigth
NSString *str =[NSString stringWithFormat:@"%f",width30];
NSDictionary *metrics = @{@"width":str};

[self.view addSubview:WFSoftAlertView];
//add the WFSoftAlertView in center(x) of superview with a padding of "width"
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-width-[WFSoftAlertView]-width-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:metrics views:views]]; 

// if you want center(y)
NSLayoutConstraint *centerY = [NSLayoutConstraint
                           constraintWithItem:WFSoftAlertView
                           attribute:NSLayoutAttributeCenterY
                           relatedBy:NSLayoutRelationEqual
                           toItem:self.view
                           attribute:NSLayoutAttributeCenterY
                           multiplier:1.0f
                           constant:0.f];
自己 到 自我观

WFSoftAlertView.translatesAutoresizingMaskIntoConstraints=NO;
NSDictionary *views = NSDictionaryOfVariableBindings(WFSoftAlertView);
float width30 = self.view.frame.size.width*.3;
width30 = width30/2; // divided by 2 for padding of its left/rigth
NSString *str =[NSString stringWithFormat:@"%f",width30];
NSDictionary *metrics = @{@"width":str};

[self.view addSubview:WFSoftAlertView];
//add the WFSoftAlertView in center(x) of superview with a padding of "width"
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-width-[WFSoftAlertView]-width-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:metrics views:views]]; 

// if you want center(y)
NSLayoutConstraint *centerY = [NSLayoutConstraint
                           constraintWithItem:WFSoftAlertView
                           attribute:NSLayoutAttributeCenterY
                           relatedBy:NSLayoutRelationEqual
                           toItem:self.view
                           attribute:NSLayoutAttributeCenterY
                           multiplier:1.0f
                           constant:0.f];