Xcode - 向 UITableView 添加约束,使其适合所有屏幕尺寸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29804323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:03:10  来源:igfitidea点击:

Xcode - Adding constraints to UITableView so that it fits all screen sizes

iosxcodeuitableviewnslayoutconstraint

提问by play2win

I've read quite a few tutorials and watched numerous videos on using constraints in Xcode. For some reason, I'm still missing certain aspects that are necessary to make the user interface look the way it's supposed to for all screen sizes.

我已经阅读了很多教程并观看了大量关于在 Xcode 中使用约束的视频。出于某种原因,我仍然缺少使用户界面看起来像所有屏幕尺寸的预期方式所必需的某些方面。

Currently, I have a UITableView laying on a view controller. The view controller is set to "Inferred" size and I have all of the different sized devices open in Assistant Editor Preview to the right so I can view the changes. I've encountered multiple problems attempting to get the constraints correct for the different screen sizes.

目前,我在视图控制器上放置了一个 UITableView。视图控制器设置为“推断”大小,我在右侧的 Assistant Editor Preview 中打开了所有不同大小的设备,以便我可以查看更改。我在尝试为不同的屏幕尺寸设置正确的约束时遇到了多个问题。

Problem 1: The UITableView has a width set to 600. This causes dead space to the right of the table view on the iPad preview and it causes the UITableView to extend too far on the smaller devices. If I make the width of the table view smaller so that it fits within the preview of all screen sizes and then pin the left and right edges of the table view to the edge of the Superview by specifying 0 and unchecking constrain to margins, the result I'm seeing in preview is that the entire table view disappears completely from each device size. I was surprised by this because I thought by pinning the table view to the margins, it would make the table view fit within each of the screens.

问题 1:UITableView 的宽度设置为 600。这会导致 iPad 预览中 table view 右侧的死区,并导致 UITableView 在较小的设备上扩展得太远。如果我缩小表格视图的宽度以使其适合所有屏幕尺寸的预览,然后通过指定 0 并取消选中约束到边距将表格视图的左右边缘固定到超级视图的边缘,结果我在预览中看到的是,每个设备尺寸的整个表格视图都完全消失了。我对此感到惊讶,因为我认为通过将 table view 固定到边距,它会使 table view 适合每个屏幕。

Problem 2: (This is a completely different scenario from Problem 1, above.) In this situation, I've left the size of the table view to 600 and just specified constraints for the internal components of the cell contained within the tableview. In this case, the table view is still slightly not wide enough for the iPad dimensions and it extends too far on the smaller devices which makes components in the cells to be truncated and off of center.

问题 2:(这是与上面的问题 1 完全不同的场景。)在这种情况下,我将表视图的大小保留为 600,并且只为包含在表视图中的单元格的内部组件指定了约束。在这种情况下,表格视图对于 iPad 尺寸仍然稍微不够宽,并且它在较小的设备上延伸得太远,这使得单元格中的组件被截断和偏离中心。

I've primarily been working in Storyboard with Any width, Any height set. However, if I change the setting to Compact width, Any height, I can alter the constraints to fit the smaller devices a little better, but there's still a problem with getting things to work between the 5.5 inch screen and the smaller devices.

我主要是在 Storyboard 中使用任意宽度、任意高度设置。但是,如果我将设置更改为 Compact width, Any height,我可以更改约束以更好地适应较小的设备,但在 5.5 英寸屏幕和较小设备之间工作仍然存在问题。

I would like to get my UITableView to extend all the way to the edges on each device and I would like to have the view inside of the cell remain centered and keep it's relative size on each of the devices. Does anyone have any suggestions on how I can accomplish this?

我想让我的 UITableView 一直延伸到每个设备的边缘,我想让单元格内部的视图保持居中并保持它在每个设备上的相对大小。有没有人对我如何做到这一点有任何建议?

回答by André Slotta

if you want to make your tableview to "fill" the whole screen (device-independent) the only thing you have to do is to pin its 4 edges (top, left, bottom, right) to its superview (the viewcontrollers view in your case) with a constant of 0. you do not specify a specific width (like in your case 600) or height.

如果你想让你的 tableview“填充”整个屏幕(独立于设备),你唯一要做的就是将它的 4 个边缘(顶部、左侧、底部、右侧)固定到它的超级视图(视图控制器视图中case) 常量为 0。您没有指定特定的宽度(如您的案例 600)或高度。

good luck :)

祝你好运 :)