使用 iOS 较大文本时,UITableViewCell 不会更改布局
UITableViewCell don't change layout when iOS larger text is used
我有一个 UITableView
如下所示:
当用户更改 iOS 文本大小设置时,布局会发生变化。
布局发生变化,详细信息标签被推到标题下方,披露指示器变得非常大。这发生在标准单元格布局和带有我自己的 UILabel
子视图的自定义单元格布局中。
我还没有准备好支持动态类型,所以有没有办法不允许 iOS 更改我的 UITableViewCells
的布局?
已经为 iOS 做了很多事情来协助 table 视图单元格中的动态类型实施 ⟹ automatic cell sizing 例如。
我在空白项目中复现了最初的问题如下:
没有额外的代码,仅由于 iOS 本机实现,请注意:
- 到达第一个辅助功能步骤时,标签会重新排序。
- 系统附件元素大小根据使用的动态类型而变化。
- 自动调整行高。
[...] is there a way to not allow iOS to change the layout of my UITableViewCells?
解决方案可能会导致创建您自己的自定义 table 视图单元格,如下所示:
class MyTableViewCell: UITableViewCell {
@IBOutlet weak var myTitle: UILabel!
@IBOutlet weak var myDetail: UILabel!
static let reuseIdentifier = "myReuseIdentifier"
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUpElementsAndConstraints()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUpElementsAndConstraints()
}
private func setUpElementsAndConstraints() {
//Custom your labels and their constraints in here.
//For the demo, everything is hard coded within the Interface Builder.
}
}
将带有 属性 adjustsFontForContentSizeCategory
的标签添加到 false 以便 keep their initial font size.
创建您自己的 'chevron image',不会像系统一样调整大小:
override func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> MyTableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: MyTableViewCell.reuseIdentifier,
for: indexPath) as! MyTableViewCell
let accessoryImageFrame = CGRect(x: 0.0, y: 0.0,
width: 40.0, height: 40.0)
let accessoryImageView = UIImageView(frame: accessoryImageFrame)
accessoryImageView.image = UIImage(named: "MyChevron")
cell.accessoryView = accessoryImageView
return cell
}
在 table 视图中定义静态行高:
override func tableView(_ tableView: UITableView,
heightForRowAt indexPath: IndexPath) -> CGFloat { return 50.0 }
不要忘记设置您的约束条件,由于 traitCollectiondidChange 方法,这些约束条件可能会根据文本大小设置进行更新...即使您不想使用动态类型功能呢。
最后,我得到了这个结果,无论设置中的文本大小如何,它都不会改变:
根据这个原理,当使用 iOS 较大的文本 时,您的 UITableViewCell 布局不会改变。 (我知道,我的自定义人字形不是很好)
我有一个 UITableView
如下所示:
当用户更改 iOS 文本大小设置时,布局会发生变化。
布局发生变化,详细信息标签被推到标题下方,披露指示器变得非常大。这发生在标准单元格布局和带有我自己的 UILabel
子视图的自定义单元格布局中。
我还没有准备好支持动态类型,所以有没有办法不允许 iOS 更改我的 UITableViewCells
的布局?
已经为 iOS 做了很多事情来协助 table 视图单元格中的动态类型实施 ⟹ automatic cell sizing 例如。
我在空白项目中复现了最初的问题如下:
没有额外的代码,仅由于 iOS 本机实现,请注意:
- 到达第一个辅助功能步骤时,标签会重新排序。
- 系统附件元素大小根据使用的动态类型而变化。
- 自动调整行高。
[...] is there a way to not allow iOS to change the layout of my UITableViewCells?
解决方案可能会导致创建您自己的自定义 table 视图单元格,如下所示:
class MyTableViewCell: UITableViewCell {
@IBOutlet weak var myTitle: UILabel!
@IBOutlet weak var myDetail: UILabel!
static let reuseIdentifier = "myReuseIdentifier"
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUpElementsAndConstraints()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUpElementsAndConstraints()
}
private func setUpElementsAndConstraints() {
//Custom your labels and their constraints in here.
//For the demo, everything is hard coded within the Interface Builder.
}
}
将带有 属性
adjustsFontForContentSizeCategory
的标签添加到 false 以便 keep their initial font size.创建您自己的 'chevron image',不会像系统一样调整大小:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> MyTableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: MyTableViewCell.reuseIdentifier, for: indexPath) as! MyTableViewCell let accessoryImageFrame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0) let accessoryImageView = UIImageView(frame: accessoryImageFrame) accessoryImageView.image = UIImage(named: "MyChevron") cell.accessoryView = accessoryImageView return cell }
在 table 视图中定义静态行高:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 50.0 }
不要忘记设置您的约束条件,由于 traitCollectiondidChange 方法,这些约束条件可能会根据文本大小设置进行更新...即使您不想使用动态类型功能呢。
最后,我得到了这个结果,无论设置中的文本大小如何,它都不会改变:
根据这个原理,当使用 iOS 较大的文本 时,您的 UITableViewCell 布局不会改变。 (我知道,我的自定义人字形不是很好)