找到你要的答案

Q:Auto-layout constraints not working as expected

Q:自动布局限制不按预期工作

I've got a UITableViewCell that has a child view, and within it an image view and a label. 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.

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.

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? 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?

我有一个UITableViewCell有儿童的观点,并在这一个图像视图和一个标签。我想用孩子的观点给周围单元格内容的一些利润的一种手段,从而给了我在细胞边缘。这似乎是很多人在线推荐做的方式。

我已经设置了我的限制如下所示:

我已经在视图控制器中的所有视图上执行了一个更新帧。故事板显示它完全一样,我希望它是在电话。当我运行它的电话虽然,我得到这个…

在这一点上我完全困惑。我花了两天时间阅读和试图布局简单UITableViewCell显然不很了解如何自动布局的作品仍然。

我只是把一切都在建议的边界(蓝线)然后告诉脚本来生成建议的约束。在这一点上的单元格的内容只是送50%它从屏幕右侧和非可视。

这样两个问题:

故事情节往往不是显示我的东西是不能准确地代表我的实际装置。这是相当常见的iOS开发?我不应该依赖在所有的脚本自动布局的代表吗?

要做的这些限制以获得细胞布局对我的设备我什么,像是对我的故事吗?我设置了什么限制?

answer1: 回答1:

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. 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.

故事情节不显示任何设备的默认内容。您可以将其设置为当前设备的大小属性(默认是“推断”)。约束用于显示所有设备上的视图相等。他们uielements根据显示的大小自动调整。因此,如果你希望你的应用程序运行在不同大小的设备上,你必须依赖于约束。

我认为你设置了太多的限制。如果你是新的自动布局。尝试阅读本指南。非常有用。

ios  uitableview  autolayout