用的UITableViewCell自动版式左页边距不同的iPhone和iPad [英] UITableViewCell with autolayout left margin different on iPhone and iPad

查看:1252
本文介绍了用的UITableViewCell自动版式左页边距不同的iPhone和iPad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是分组的UITableView静态细胞期权屏幕/景象。一切都在X $ C $ç6.1 /的iOS 8.1.x /故事板采用自动布局完成。内的表组有混合类型的细胞和有两种类型,导致我的问题:

I am using a grouped UITableView with static cells for an options screen/scene. Everything is done in XCode 6.1 / iOS 8.1.x / Storyboard using Autolayout. Within the table groups there are mixed types of cells and there are two types that cause me problems:


    与自定义样式和
  1. 单元

  2. 风格单元格格式右详细信息

在小区#1我可以为标签和领先的集装箱之间的左边距设置的约束。在小区2#我无法设置在Interface Builder AFAIK任何约束上。我已经设置在小区#1中的标签上的左边界所以它与在小区#2中的标签对齐。一切看起来都在iPhone上很好,但如果我告诉在iPad相同的表,其中的tableview的容器的大小是成功的一半屏幕大小,电池#2获得了更多余量(动态?),而小区#1保持绝对的保证金我设置的约束。我也试图更改单元格#1的左边距与属性相对于保证金,但无济于事。

On cell #1 I can set a constraint for the left margin between the label and the leading container. On cell #2 I cannot set any contraints in Interface Builder AFAIK. I have set the left margin on the label in cell #1 so it aligns with the label in cell #2. Everything looks fine on an iPhone, but if I show the same table on an iPad where the size of the tableview's container is half the screensize, cell #2 gets more margin (dynamically?) while cell #1 maintains the absolute margin I set in the constraints. I also tried to change the left margin in cell #1 with the attribute "relative to margin" but to no avail.

iPhone:

iPad的(带的tableview宽度= 1/2屏幕大小)

iPad (with tableview width = 1/2 screen size)

所以现在的问题是:我如何设置标签的限制在小区#1,使其像对齐单元#2

So the question is: How do I set the constraints for the label in cell #1 so that it aligns like cell #2.

下面也是一个X $ C $ 6.1Ç示例项目演示问题的链接。 iPhone和iPad上运行,看的区别:

Here is also a link to a Xcode 6.1 sample project demonstrating the problem. Run on iPhone and iPad to see the difference:

<一个href=\"https://dl.dropboxusercontent.com/u/5252156/$c$c/tableViewTest.zip\">https://dl.dropboxusercontent.com/u/5252156/$c$c/tableViewTest.zip

此问题可能与<一个href=\"http://stackoverflow.com/questions/16619435/layout-static-table-cell-for-iphone-and-ipad\">Layout iPhone和iPad 静态表格单元格,但它也可能因为一切都应该现在是自适应的差异,iOS版8。这就是为什么我决定无论如何张贴了这个问题。

This question might be related to Layout static table cell for iPhone and iPad, but it might also differ for iOS 8 since everything is supposed to be adaptive now. That's why I decided to post this question anyway.

推荐答案

与苹果的bug报告团队与回答许多样本项目和截图和解剖战斗后,我发现该解决方案让您的自定义式的细​​胞行为一致对他们的利润,并就像默认UITableViewCells,你必须做以下(大多是基于的贝基的回答,我已经强调什么是不同的,是什么让这对我的工作):

 How to fix it

After fighting with the apple bug reporting team with many sample projects and screenshots and dissecting that answer, I've found that the solution to have your custom-style cells behave consistently regarding their margins and be just like the default UITableViewCells, you have to do the following (mostly based on Becky's answer, I've highlighted what's different and what made it work for me) :


  1. 选择您的手机的内容视图在IB

  2. 转到尺寸检查

  3. 在布局边距部分,检查preserve的SuperView边距(不要点击加号)

  1. Select your cell's content view in IB
  2. Go to the Size Inspector
  3. In the Layout Margins section, check Preserve Superview Margins (do not click the plus-sign)

检查preserve上海华利润率

(而这里的关键)做同样的作为细胞本身(内容视图的父,如果你会)

(And here's the key) Do the same for the cell itself (the content view's parent if you will)

细胞,而不是内容查看这次

设置您的约束条件如下:Label.Leading = Superview.Leading保证金(为0的常数)

Setup your constraints as follows : Label.Leading = Superview.Leading Margin (with a constant of 0)

设置为自定义单元格的标签约束

现在所有的细胞都会有自己的标签使用默认的细胞相一致!这对我的作品在X code 7和上,它包括在我提到线程提及该修补程序。 IB和模拟器应该立即显示正确对齐的标签。

Now all your cells will have their label consistent with the default cells! This works for me in Xcode 7 and up and it includes the fix mentioned in the thread I referred to. IB and the simulator should now show properly aligned labels.

最终结果在模拟器

您也可以做一些这方面编程,例如在视图控制器的类:

You could also do some of this programmatically, for example in the View Controller's class :

cell.preservesSuperviewLayoutMargins = true
cell.contentView.preservesSuperviewLayoutMargins = true

或者你可以把它设置致电UIAppearance一次启动时(我只知道斯威夫特,不好意思):

Or you could have it set up by calling UIAppearance once at startup (I only know Swift, sorry) :

UITableViewCell.appearance().preservesSuperviewLayoutMargins = true
UITableViewCell.appearance().contentView.preservesSuperviewLayoutMargins = true


如何以及为什么它的工作原理

由于伊桑好心人士指出,苹果公司自己的UIView的文档描述了 preservesSuperviewLayoutMargins 如下:


How and why it works

As Ethan kindly pointed out, Apple's own documentation on UIView describes preservesSuperviewLayoutMargins as follows :

在此属性的值为真正,铺设内容时,上海华的利润率也算。此裕度影响的布局,其中的图的边缘,它的父之间的距离大于相应的余量越小。例如,你可能有它的框架precisely它的父的界限相匹配的内容视图。当任一上海华的利润率是通过重新内容视图和自身的边距psented $ P $的区域内,UIKit的调整内容视图的布局尊重上海华的利润率。调整的量是需要的,以确保内容的最小量也是上海华的利润率内

When the value of this property is true, the superview’s margins are also considered when laying out content. This margin affects layouts where the distance between the edge of a view and its superview is smaller than the corresponding margin. For example, you might have a content view whose frame precisely matches the bounds of its superview. When any of the superview’s margins is inside the area represented by the content view and its own margins, UIKit adjusts the content view’s layout to respect the superview’s margins. The amount of the adjustment is the smallest amount needed to ensure that content is also inside the superview’s margins.

因此​​,如果你希望你的单元格的内容与在 TableView中的利润率保持一致(这是曾祖如果你愿意),你需要有你的内容的两位长辈,内容的浏览和表格单元格本身,preserve自己的SuperView的边缘。

Therefore, if you want your cell's content to align with the TableView's margins (it's great-grandparent if you will), you need to have your content's two ascendants, Content View and the Table Cell itself, preserve the margins of their own superview.

为什么这不是默认行为让我惊讶。我觉得不想定制一切期望这种继承默认情况下,谁大多数开发商

Why this isn't default behavior surprises me : I feel like most developers who don't want to customize everything would expect this "inheritance" by default.

这篇关于用的UITableViewCell自动版式左页边距不同的iPhone和iPad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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