简体   繁体   English

自动布局约束未按预期工作

[英]Auto-layout constraints not working as expected

I've got a UITableViewCell that has a child view, and within it an image view and a label. 我有一个具有子视图的UITableViewCell,并且在其中有一个图像视图和一个标签。 I am wanting to use the child view as a means of giving some margin around the contents of the cell, thus giving me margins inbetween cells. 我想将子视图用作在单元格内容周围留出一些边距的方法,从而在单元格之间留出余量。 This seemed to be the way a lot of people online recommended doing it. 这似乎是许多在线人建议这样做的方式。

I've set up my constraints as shown below: 我设置了约束,如下所示:

在此处输入图片说明

I have performed a Update Frames on all views in the view controller. 我已经在视图控制器中的所有视图上执行了更新框架 The story board shows it exactly as I'm expecting it to be on the phone. 故事板完全按照我期望的方式将其显示在电话中。 When I run it on the phone though, I get this... 当我在手机上运行它时,我得到了...

在此处输入图片说明

I'm completely baffled at this point. 在这一点上,我完全感到困惑。 I've spent two days of reading and trying to layout a simple UITableViewCell and clearly don't have a good understanding of how auto-layout works still. 我花了两天的时间阅读并尝试布局一个简单的UITableViewCell,显然对自动布局的工作方式还没有很好的了解。

I've even just laid everything out along the suggested boundaries (the blue lines) and then told the storyboard to generate suggested constraints. 我什至只是沿着建议的边界(蓝线)布置了所有内容,然后告诉情节提要生成建议的约束。 At which point the content of the cell just sent with 50% of it off the right side of the screen and un-viewable. 此时,该单元格的内容刚刚发送出去,其中50%的内容不在屏幕右侧,并且无法查看。

So two questions: 有两个问题:

The storyboard more often than not shows me something that is not accurately represented on my actual device. 故事板经常会向我显示在我的实际设备上无法准确显示的内容。 Is this fairly common in iOS development? 这在iOS开发中相当普遍吗? Should I not be relying at all on the storyboard auto-layout representation? 我应该完全不依赖情节提要自动布局表示吗?

What do I have to do to these constraints in order to get the cells to layout on my device, like it is shown in my storyboard at this time? 为了使这些单元在我的设备上进行布局(如此时在情节提要中显示的那样),我必须对这些约束做什么? What constraints am I setting wrong? 我设定了什么限制?

Storyboard doesn't display the content according to any device by default. 情节提要默认情况下不会根据任何设备显示内容。 You can set it to your current device in its size properties(by default it is "Inferred"). 您可以通过其尺寸属性将其设置为当前设备(默认情况下为“推断”)。 Constraints are used to display the views equal on all devices. 约束用于显示所有设备上相等的视图。 They automatically adjust UIelements according to display size. 它们根据显示大小自动调整UIelements。 So if you want your app to run on devices of different sizes you have to rely on constraints. 因此,如果您希望您的应用程序在不同大小的设备上运行,则必须依赖约束。

I think you are setting too many constraints. 我认为您设置了太多约束。 Happens if you are new to auto layout. 如果您不熟悉自动布局,则会发生。 Try reading this guide. 尝试阅读指南。 Its very helpful. 它非常有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM