简体   繁体   English

分组UITableView和水平边距

[英]Grouped UITableView and horizontal margins

I have a grouped UITableView that was really designed to look nice in portrait mode for iPhone. 我有一个分组的UITableView,它真的被设计成在iPhone的纵向模式下看起来很漂亮。 Its cell subviews have autosizing set up so that they stretch in landscape mode, but this makes it a lot less aesthetically pleasing -- the cells just look too wide for their content. 它的单元子视图具有自动调整设置,以便它们在横向模式下伸展,但这使得它在美学上不那么令人愉悦 - 细胞看起来对于它们的内容来说太宽了。

I'm now making it a universal app but on iPad the autosizing causes even more stretching and it looks just unacceptable. 我现在正在制作一个通用的应用程序,但在iPad上,自动调整会导致更多的拉伸,看起来是不可接受的。

It would be ideal if I could make the UITableView's groups of cells have a fixed width (or a max width), or if I could somehow control the horizontal margins. 如果我可以使UITableView的单元格组具有固定的宽度(或最大宽度),或者如果我可以以某种方式控制水平边距,那将是理想的。

Having not found support for this in UITableView, I have done a few quick attempts at subclassing it to constrain its size at layout time and, as an alternative, at introducing a container view in order to make the UITableView autoresize vertically only. 在UITableView中没有找到对此的支持,我已经做了一些快速的尝试,在子类化它以限制它在布局时的大小,并且作为替代方案,引入容器视图以使UITableView仅垂直自动调整大小。 Both approaches work but create new problems: Scrolling doesn't work when swiping in the margins, and I am now forced to make the UITableView's background transparent (which goes against Apple's recommendations) as there is now a discontinuity of background between the UITableView's frame and the margins. 这两种方法都有效但会产生新的问题:在边距中滑动时滚动不起作用,我现在被迫使UITableView的背景透明(这违背了Apple的建议),因为现在UITableView的框架之间存在背景的不连续性。利润率。

Has anyone found a trick to solve my problem (ie constrain the width of the groups in a UITableView, causing margins to expand to fill the width of the view), or an open source solution to it? 有没有人找到解决我的问题的技巧(即约束UITableView中组的宽度,导致边距扩大以填充视图的宽度),或者是一个开源解决方案?

Good news! 好消息! I finally found a way to achieve this satisfactorily with only tiny code changes: 我终于找到了一种方法,只需很少的代码更改即可圆满完成:

You can always keep the standard table view and provide custom backgrounds with transparent sides for the table view cell's so that they look smaller than they are. 您始终可以保留标准表格视图,并为表格视图单元格提供透明边的自定义背景,以使它们看起来比它们小。

Cocoa With Love has a great article on how to do that here: Easy custom UITableView drawing . Cocoa With Love在这里有一篇关于如何做到这一点的好文章: 简单的自定义UITableView绘图

The basic gist of the article is that you need to make six different versions of the backgrounds, and supply the correct one when tableView:cellForRowAtIndexPath: asks for a cell. 本文的基本要点是,您需要制作六个不同版本的背景,并在tableView:cellForRowAtIndexPath:请求单元格时提供正确的背景。 You will need one with rounded corners at the top (for the first row of a section), one with rounded corners at the bottom (for the bottom row of a section), and one with all four corners rounded (for when there is only one row in the section). 您将需要一个在顶部有圆角(一个部分的第一行),一个在底部有圆角(对于一个部分的底行),一个有四个角的圆角(当只有一个时)该部分中的一行)。 Then you will need the same three, but customized for the "selected" version of each row. 然后你将需要相同的三个,但为每行的“选定”版本定制。

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

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