在UITableView的单元格中设置边距可以通过以下几种方式实现:
class CustomTableViewCell: UITableViewCell {
override func layoutSubviews() {
super.layoutSubviews()
contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
}
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.contentView.frame = cell.contentView.frame.inset(by: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
}
class CustomTableViewCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
let margin: CGFloat = 10
contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: margin).isActive = true
contentView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -margin).isActive = true
contentView.topAnchor.constraint(equalTo: topAnchor, constant: margin).isActive = true
contentView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -margin).isActive = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
以上是在UITableView的单元格中设置边距的几种方法。根据具体的需求和使用场景,选择适合的方法来实现边距的设置。
腾讯云相关产品和产品介绍链接地址:
没有搜到相关的沙龙
领取专属 10元无门槛券
手把手带您无忧上云