【问题标题】:UITableViewCell don't change layout when iOS larger text is used使用 iOS 大文本时,UITableViewCell 不会更改布局
【发布时间】:2020-10-30 16:14:18
【问题描述】:

我有一个UITableView,如下所示:

当用户更改 iOS 文本大小设置时,布局会发生变化。

在标题下方的详细标签被推送的地方,布局发生了变化,并且披露指示器变得非常大。这发生在标准单元格布局和自定义单元格布局以及我自己的 UILabel 子视图中。

我还没有准备好支持动态类型,那么有没有办法让 iOS 改变我的UITableViewCells 的布局?

【问题讨论】:

    标签: ios uitableview accessibility dynamic-type-feature


    【解决方案1】:

    已经为 iOS 做了很多事情来帮助在表格视图单元格中实现动态类型 ⟹ automatic cell sizing 例如。

    我在一个空白项目中重现了最初的问题,如下所示:

    没有额外的代码,仅由于 iOS 原生实现,请注意:

    • 当达到第一个辅助功能步骤时,标签会重新排序。
    • 系统附件元素大小根据使用的动态类型而变化。
    • 可以自动调整行高。

    [...] 有没有办法让 iOS 更改我的 UITableViewCells 的布局?

    一种解决方案可能会导致创建您自己的自定义表格视图单元格,如下所示:

    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.
        }
    }
    
    1. 将带有属性adjustsFontForContentSizeCategory 的标签添加到false,以便keep their initial font size

    2. 创建您自己的“人字形图像”,不会像系统一样调整大小:

       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
       }
      
    3. 在表格视图中定义一个静态行高:

       override func tableView(_ tableView: UITableView,
                               heightForRowAt indexPath: IndexPath) -> CGFloat { return 50.0 }
      
    4. 不要忘记设置可以根据文本大小设置更新的约束,这要归功于traitCollectiondidChange 方法...即使您还不想使用动态类型功能。 ?

    最后,我得到这个结果,无论设置中的文本大小如何,我都不会改变:?

    根据这个原理,您的 UITableViewCell 布局在使用 iOS 大文本时不会改变。 ?(我知道,我的自定义 V 形不是很好?)

    【讨论】:

      猜你喜欢
      • 2011-10-18
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 2016-03-24
      相关资源
      最近更新 更多