Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.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
在iOS应用程序中添加自动布局子视图时高度错误_Ios_Objective C_Iphone_Autolayout - Fatal编程技术网

在iOS应用程序中添加自动布局子视图时高度错误

在iOS应用程序中添加自动布局子视图时高度错误,ios,objective-c,iphone,autolayout,Ios,Objective C,Iphone,Autolayout,我正在尝试将子视图添加到现有视图中。我基本上是添加一个子视图来模拟操作表,如以下示例所示: 只要我不在自定义操作表上使用autolayout,这个示例就可以正常工作。当我这样做的时候,我会遇到一些问题。我正在添加的子视图由一个红色视图组成。此红色视图对其高度、水平和垂直空间具有约束 (来源:) 我的问题是,4S和5S设备的高度似乎不正确。5S的视野比4S的高。我本来以为是相反的,因为5S比4S得分多 (来源:) 我使用以下代码添加子视图: [self.view addSubview:sel

我正在尝试将子视图添加到现有视图中。我基本上是添加一个子视图来模拟操作表,如以下示例所示:

只要我不在自定义操作表上使用autolayout,这个示例就可以正常工作。当我这样做的时候,我会遇到一些问题。我正在添加的子视图由一个红色视图组成。此红色视图对其高度、水平和垂直空间具有约束


(来源:)

我的问题是,4S和5S设备的高度似乎不正确。5S的视野比4S的高。我本来以为是相反的,因为5S比4S得分多


(来源:)

我使用以下代码添加子视图:

[self.view addSubview:self.customActionSheet.view];
[self.customActionSheet视图将显示:否]

如果我改为通过按下视图来添加视图:

[self.navigationController pushViewController:self.customActionSheet动画:是]

结果和我预期的一样。4S完全被红色视图覆盖,5S在红色视图上方仍有一些区域


我做错了什么?

您的视图不会根据设备屏幕自动调整大小<代码>[UINavigationController pushViewController:animated:
将起作用,因为导航控制器为您设置视图的框架。快速的方法是使用当前视图设置视图的框架

self.customActionSheet.view.frame = self.view.bounds;
并且不要调用
视图将出现:
您自己,系统将调用它

我的建议是使用自动布局来实现对superview的查看。以下代码使用PureLayout:


问题是您需要将约束添加到
self.customActionSheet.view

当您通过
pushViewController:animated:
消息加载控制器视图时,系统会向视图添加所需的约束,以填充所有屏幕空间

相反,如果您手动添加它(通过
addSubview:
message),autoulayouts约束不会自动创建,您需要以编程方式创建并添加到视图中

我在这里发布了一个您可能需要的示例,以便
customActionSheet
填满所有屏幕空间:

NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:self.view
                                                                      attribute:NSLayoutAttributeLeft
                                                                      relatedBy:NSLayoutRelationEqual
                                                                         toItem:self.customActionSheet.view
                                                                      attribute:NSLayoutAttributeLeft
                                                                     multiplier:1.0
                                                                       constant:0];

NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.view
                                                                      attribute:NSLayoutAttributeTop
                                                                      relatedBy:NSLayoutRelationEqual
                                                                         toItem:self.customActionSheet.view
                                                                      attribute:NSLayoutAttributeTop
                                                                     multiplier:1.0
                                                                       constant:0];

NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:self.view
                                                                      attribute:NSLayoutAttributeBottom
                                                                      relatedBy:NSLayoutRelationEqual
                                                                         toItem:self.customActionSheet.view
                                                                      attribute:NSLayoutAttributeBottom
                                                                     multiplier:1.0
                                                                       constant:0];


NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:self.view
                                                                  attribute:NSLayoutAttributeRight
                                                                  relatedBy:NSLayoutRelationEqual
                                                                     toItem:self.customActionSheet.view
                                                                  attribute:NSLayoutAttributeRight
                                                                 multiplier:1.0
                                                                   constant:0];

[self.view addSubview:self.customActionSheet.view];
[self.view addConstraints:@[left, right, top, bottom]];

是否使用自动布局或自动调整大小约束已添加到xib文件中。很抱歉没有解释。
NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:self.view
                                                                      attribute:NSLayoutAttributeLeft
                                                                      relatedBy:NSLayoutRelationEqual
                                                                         toItem:self.customActionSheet.view
                                                                      attribute:NSLayoutAttributeLeft
                                                                     multiplier:1.0
                                                                       constant:0];

NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.view
                                                                      attribute:NSLayoutAttributeTop
                                                                      relatedBy:NSLayoutRelationEqual
                                                                         toItem:self.customActionSheet.view
                                                                      attribute:NSLayoutAttributeTop
                                                                     multiplier:1.0
                                                                       constant:0];

NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:self.view
                                                                      attribute:NSLayoutAttributeBottom
                                                                      relatedBy:NSLayoutRelationEqual
                                                                         toItem:self.customActionSheet.view
                                                                      attribute:NSLayoutAttributeBottom
                                                                     multiplier:1.0
                                                                       constant:0];


NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:self.view
                                                                  attribute:NSLayoutAttributeRight
                                                                  relatedBy:NSLayoutRelationEqual
                                                                     toItem:self.customActionSheet.view
                                                                  attribute:NSLayoutAttributeRight
                                                                 multiplier:1.0
                                                                   constant:0];

[self.view addSubview:self.customActionSheet.view];
[self.view addConstraints:@[left, right, top, bottom]];