swift 自动调整表视图
来源:互联网 发布:三国乱世战车升级数据 编辑:程序博客网 时间:2024/05/22 08:19
在iOS 7开发人员引入动态类型的概念——允许用户通过设置面板在程序中修改字体大小。这真是强大,可以大大改善用户体验为用户提供不同品质的视线。
然而,有一个很大的问题与此相关,在表视图的形式。虽然类型的大小可以改变,这是由开发人员定义的cell高度的表视图。这意味着要么内置值,或者动态地计算它们,都不很容易。当然一定有更好的方法吗?
在iOS 8终于可以表视图细胞可autosize本身。在这个项目中,我们将演示与动态类型和自定义相关的cell,看一看实现它是多么容易。
Using the ‘stock’ table view cells
When you first create a table view, chances are you’ve just used the stock table view cells - ofwhich there are 4 styles (basic, left detail, right detail, and subtitle). In iOS 8, the labels in the baseUITableViewCellare pre-configured for dynamic type. This means that they adapt to the text sizespecified in the device settings panel.
当您首先创建一个表视图,那么你只是使用了股票表视图细胞——其中有4风格(基本、左细节,正确的细节,和副标题)。在iOS 8中,标签在基地UITableViewCell预先配置的动态类型。这意味着他们适应文字大小设置面板中指定的设备。
Since these cells are also laid out using autolayout, they will autosize to fit the differing sizes of text.
This actually means that you get auto-sizing table view cellsfor freeif you just use the stock cells.
To see this in action run up the accompanyingMagicTableapp:
If you then use the settings to change the text size (in the same way as iOS7):
And then return to theMagicTableapp then you’ll be able to see the effect:
This is all well, and good, but more often than not you’ll want to create your own custom table cells.In order to do this, you need to understand a little bit more about how the auto sizing actually works- let’s take a look at that next.
Creating custom table view cells
Traditionally setting the row height for a table view cell would be done on the table - using therowHeightproperty. In order to vary the row size on a per-row basis, you would use the table viewdelegate methodestimatedHeightForRowAtIndexPath:to return a different height for each row.
The problem for this approach is that you either need to know the row height at compile time, orcalculate the row height for each row at run-time. The delegate method is called when the tableviewfirst appears, which means that you need to calculate the cell height beforethe cells are created. Inorder to do this you can end up writing layout code twice - once for size calculations and once fordisplay. This process can take a long time to perform - and involves entirely up-front calculations.
iOS 7 introducedestimatedRowHeight, which transformed the row-height requests into lazycalculations - only requesting the height for a row once it is about to be displayed on the screen.However, it still required you to calculate the row height yourself.
In iOS 8, you can still use these approaches, however, cells can now be responsible for their own sizing- via autolayout. This is both great from an ease-of-use perspective and also from a software designangle. A cell is responsible for its own layout, so it makes sense that it should also be responsible fordetermining its own height.
在iOS 8中,你仍然可以使用这些方法,然而,cell现在可以自己的大小——通过autolayout负责。这既是伟大的从易用性的角度来看,也从软件设计的角度。cell负责自己的布局,所以它是有意义的,它也应该负责确定自己的高度。
It’s actually pretty easy to get auto-cell height working for a custom cell. The most important part is that your constraints properly define the height of the cell.
其实很容易得到auto-cell高度为一个自定义的工作单元。最重要的部分是你的约束正确定义单元格的高度。
You must provide an estimated height for the rows, and if you provide an actual height (either viathe property on the table, or via the delegate) then this will override any calculated cell size. In orderto specify that you haven’t set a cell height, use the UITableViewAutomaticDimensionconstant:
行你必须提供一个估计的高度,如果你提供一个实际高度(通过属性表,或通过代表),那么这将会覆盖任何计算单元尺寸。为了指定你还没有设定一个细胞高度,使用UITableViewAutomaticDimension常数:
tableView.rowHeight=UITableViewAutomaticDimension
The constraints need to relate to thecontentViewwithin a UITableViewCell, and can be set up incode or in IB. The accompanying project sets up constraints in IB:
In order to demo the different heights, the datasource for the table inMagicTable, changes the font height of the custom label depending on the label:
Conclusion
Auto-sizing table view cells is something that developers have longed for, and it’s great news thatiOS 8 introduces this functionality. In many cases, since you should already be using auto-layout,you’ll just get this functionality for free - just a matter of not specifying cell heights. It’s definitelyworth the time to go and ensure that your existing tableviews support this behavior.
- swift 自动调整表视图
- iOS automaticallyAdjustsScrollViewInsets(自动调整滚动视图)使用
- swift UITableView 表视图
- IOS 代码实现自动布局(视图尺寸自动调整)
- Swift UIScrollView滚动视图/无限循环滚动/自动滚动
- Swift 表视图动画教程: Drop-In Cards
- Swift UIAlertView(警告视图)、UIActionSheet(操作表)
- Swift UIView 层次调整
- swift UIView层次调整
- DreamWeaver代码视图调整
- 滚动视图的调整
- Myeclipse视图调整
- swift 视图间传值
- Swift ImageView图像视图
- Swift UIScrollView(滑动视图)
- swift 视图转换 模态框
- iOS弹出视图-swift
- 调整代码, 加速swift编译
- 算法导论 第14章 数据结构的扩张(一)动态顺序统计树
- android系统中获取imei号 手机号码 imsi号码
- 日志级别的选择:Debug、Info、Warn、Error还是Fatal
- 处理【Fatal error: Can't use function return value in write context 】的情况之《empty的解析》
- 最长公共子序列(LCS)问题
- swift 自动调整表视图
- js实现垂直滚动效果
- AO开发缩放到图层
- WPF之DataGrid应用
- struts包下载
- SQL+Web 无法加载驱动
- Hibernate文档
- Java基础(1)——break、continue
- 工作周报029