swift 如何计算不同移动的尺寸的手机长宽比?

mpbci0fu  于 2023-02-03  发布在  Swift
关注(0)|答案(1)|浏览(125)

我需要帮助!我如何计算位于tableView单元格中的视图的纵横比,此视图应显示不同的设备(17:10)。我有一个包含一定数量单元格的表格。我将任意表格的单元格长度设置为226.0。这样做是为了显示单元格之间24个点的任意距离。(无故事板)

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 226.0
}

在单元格内部,我有一个高度为(17:10)的视图,我使用SnapKit作为约束。

cellView.snp.makeConstraints { make in
      make.top.equalTo(self.contentView.snp.top)
      make.leading.equalTo(self.contentView.snp.leading)
      make.trailing.equalTo(self.contentView.snp.trailing
      make.height.equalTo(self.contentView.snp.width).multipliedBy(10.0 / 17.0)
}

对于测试,我使用iPhone SE(第三代),运行应用程序后,长宽比只适用于此模型,它有24点之间的距离细胞.如果你运行其他模拟器模型,如iPhone 14 Pro max,细胞粘在一起.我怎么计算,使不同的模型之间的距离始终保持24点?

    • 编辑**

SE第3代模拟器结果
正如你在截图中看到的,这个模拟器对应的长宽比是17:10,加上其他单元格之间的距离是24点。但是如果你运行一个更大的模型的模拟器,那么单元格会粘在一起。

ajsxfq5m

ajsxfq5m1#

你想让单元格设置自己的高度,所以...
彻底摆脱这个:

//func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//    return 226.0
//}

按如下方式配置单元格类:

class AspectCell: UITableViewCell {
    
    let cellView = UIView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        commonInit()
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }
    private func commonInit() {
        cellView.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(cellView)
        
        cellView.snp.makeConstraints { make in
            // use Priority of Required-1 to avoid autolayout complaints
            make.top.bottom.equalTo(self.contentView).inset(12.0).priority(.init(999.0))
            make.leading.trailing.equalTo(self.contentView).inset(12.0)
            make.height.equalTo(self.contentView.snp.width).multipliedBy(10.0 / 17.0)
        }

        cellView.backgroundColor = .systemRed
        cellView.layer.cornerRadius = 12
    }
    
}

下面是一个基本的表视图控制器,可以查看结果:

class AspectTableViewController: UITableViewController {
    
    let colors: [UIColor] = [
        .systemRed, .systemGreen, .systemBlue,
        .systemYellow, .green, .blue,
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.separatorStyle = .none
        tableView.register(AspectCell.self, forCellReuseIdentifier: "aspectCell")
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let c = tableView.dequeueReusableCell(withIdentifier: "aspectCell", for: indexPath) as! AspectCell
        c.cellView.backgroundColor = colors[indexPath.row % colors.count]
        return c
    }
    
}

输出:
第一节第一节第一节第一节第一次

编辑- * 以阐明约束优先级... *

在单元格(和其他视图)中使用动态大小约束时,一个非常常见的情况是最终得到一个不太有效的值。
例如,在aniPhone 14 Pro(cellView,每边有12个点)上运行此命令:

  • 像元的实际宽度为393
  • cellView子视图的实际宽度为369(393 - 24)
  • 369.0 x 10.0 / 17.0 = 217.05882352941177美元

自动布局将向调试控制台打印警告,因为它无法设置217.05882352941177的高度...它将使用217
通过给Bottom约束一个小于所需的优先级,我们告诉自动布局我们可以使用217(也就是说,它是***不需要的***)。
这是一个更好的makeConstraints块(我们不需要降低顶部约束的优先级),并且上面的代码使用了contentView宽度而不是cellView宽度,所以它的长宽比不是17:10:

cellView.snp.makeConstraints { make in
        // 12-points on each side
        make.leading.trailing.equalTo(self.contentView).inset(12.0)

        // 12-points from the top
        make.top.equalTo(self.contentView).inset(12.0)

        // proportional height (17:10)
        make.height.equalTo(cellView.snp.width).multipliedBy(10.0 / 17.0)

        // use Priority of Required-1 to avoid autolayout complaints
        make.bottom.equalTo(self.contentView).inset(12.0).priority(.init(999.0))
    }

相关问题