Xcode - 向 UITableView 添加约束,使其适合所有屏幕尺寸 [英] Xcode - Adding constraints to UITableView so that it fits all screen sizes

查看:55
本文介绍了Xcode - 向 UITableView 添加约束,使其适合所有屏幕尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

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.

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

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.

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

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.

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

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?

推荐答案

如果你想让你的 tableview填满"整个屏幕(与设备无关),你唯一需要做的就是固定它的 4 个边缘(顶部,左侧,底部,右侧)到其超级视图(在您的情况下为视图控制器视图),常量为 0.您没有指定特定的宽度(如您的情况 600)或高度.

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.

祝你好运:)

这篇关于Xcode - 向 UITableView 添加约束,使其适合所有屏幕尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆