我正在尝试在tableview单元格内舍入自定义视图的底角。我正在使用的代码无法正常工作,我也不明白为什么...
因此,我有一个UItableViewCell
,在contentView
内有一个自定义视图containerView
。在其中,顶部是两个标签,底部是另一个视图。该视图必须具有圆角。
我的代码:
class HomeTableViewCell: UITableViewCell {
var containerView: UIView!
var bottomDetailsView: UIView!
var titleLabel: UILabel!
var timeLabel: UILabel!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: "tableCell")
containerView = UIView()
bottomDetailsView = UIView()
timeLabel = UILabel()
titleLabel = UILabel()
self.contentView.addSubview(containerView)
containerView.addSubview(bottomDetailsView)
containerView.addSubview(titleLabel)
containerView.addSubview(timeLabel)
containerView.snp.makeConstraints({ (maker) in
maker.top.equalTo(self.contentView.snp.top)
maker.bottom.equalTo(self.contentView.snp.bottom)
maker.left.equalTo(self.contentView.snp.left).inset(10)
maker.right.equalTo(self.contentView.snp.right).inset(10)
})
bottomDetailsView.snp.makeConstraints { (maker) in
maker.top.equalTo(containerView.snp.centerY).offset(30)
maker.left.equalTo(containerView.snp.left)
maker.right.equalTo(containerView.snp.right)
maker.bottom.equalTo(containerView.snp.bottom)
}
timeLabel.snp.makeConstraints { (maker) in
maker.centerX.equalTo(containerView.snp.centerX)
maker.centerY.equalTo(containerView.snp.centerY).offset(10)
}
titleLabel.snp.makeConstraints { (maker) in
maker.left.equalTo(containerView.snp.left).inset(10)
maker.top.equalTo(containerView.snp.top).inset(10)
maker.right.equalTo(containerView.snp.right).inset(10)
}
}
override func layoutSubviews() {
super.layoutSubviews()
bottomDetailsView.backgroundColor = #colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)
containerView.backgroundColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
containerView.layer.cornerRadius = 15.0
timeLabel.font = timeLabel.font.withSize(24)
timeLabel.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
titleLabel.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
let path = UIBezierPath(roundedRect: bottomDetailsView.bounds, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: 15, height: 15))
let shapeLayer = CAShapeLayer()
shapeLayer.frame = bottomDetailsView.frame
shapeLayer.path = path.cgPath
bottomDetailsView.layer.backgroundColor = UIColor.red.cgColor
bottomDetailsView.layer.mask = shapeLayer
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我正在使用SnapKit
进行自动布局。有问题的代码是
let path = UIBezierPath(roundedRect: bottomDetailsView.bounds, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: 15, height: 15))
let shapeLayer = CAShapeLayer()
shapeLayer.frame = bottomDetailsView.frame
shapeLayer.path = path.cgPath
bottomDetailsView.layer.backgroundColor = UIColor.red.cgColor
bottomDetailsView.layer.mask = shapeLayer
我尝试使用此代码进行不同的组合,但无法正常工作。它绝对不会显示任何内容。如果我写的是bottomDetailsView.layer.mask = shapeLayer
而不是bottomDetailsView.layer.addSublayer(shapeLayer)
,则显示的是视图,但没有四角。
我不记得自己做了什么,但是在某个时候,我意识到我在视图顶部看到了一个黑色的圆形层,但前提是该单元格位于屏幕之外,然后再次加载了{{1} }。我在互联网上找到的每个解决方案都指向我拥有的相同代码,也许必须在其他地方完成?
编辑1:
得到答案后,我更新了代码,现在看起来像这样:
dequeueReuseableCell
我也按照Duncan C的建议做了UIView的子类:
class HomeTableViewCell: UITableViewCell {
var containerView: UIView!
var bottomDetailsView: BottomDetailsView!
var titleLabel: UILabel!
var timeLabel: UILabel!
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: "tableCell")
containerView = UIView()
bottomDetailsView = BottomDetailsView(frame: .zero)
timeLabel = UILabel()
titleLabel = UILabel()
self.contentView.addSubview(containerView)
containerView.addSubview(titleLabel)
containerView.addSubview(timeLabel)
containerView.addSubview(bottomDetailsView)
containerView.snp.makeConstraints({ (maker) in
maker.top.bottom.left.right.equalToSuperview()
})
titleLabel.snp.makeConstraints { (maker) in
maker.height.equalTo(15)
maker.top.left.right.equalTo(containerView)
maker.bottom.equalTo(timeLabel.snp.top)
}
timeLabel.snp.makeConstraints { (maker) in
maker.height.equalTo(34)
maker.top.equalTo(titleLabel.snp.bottom)
maker.centerX.equalTo(containerView.snp.centerX)
maker.bottom.equalTo(bottomDetailsView.snp.top)
}
bottomDetailsView.snp.makeConstraints { (maker) in
maker.top.equalTo(timeLabel.snp.bottom)
maker.left.equalTo(containerView.snp.left)
maker.right.equalTo(containerView.snp.right)
maker.bottom.equalTo(containerView.snp.bottom)
}
}
override func layoutSubviews() {
super.layoutSubviews()
bottomDetailsView.backgroundColor = #colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)
containerView.backgroundColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
containerView.layer.cornerRadius = 15.0
timeLabel.font = timeLabel.font.withSize(24)
timeLabel.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
titleLabel.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
titleLabel.numberOfLines = 0
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
现在一切正常,除了以下事实:如果我放大,如果我努力看的话,我可以在角落看到一点点下面的视图。它们没有完全对齐。底线还可以,就在角落。
答案 0 :(得分:1)
当您提供containerView时,您的约束不足以布置单元格的子视图
顶部,左侧,右侧,底部
和标签timeLabel
centerX,Y
titleLabel
左,右,上
如果您查看这些约束,则没有从上到下垂直创建约束from
,单元格期望它是其子视图containeView
的高度,但是您要进行相反的操作,因此您需要挂钩标签titleLabel
到容器的顶部(您已完成),它在标签timeLabel
的底部,容器的底部到容器的底部,并且容器的底部已经被钩到{{1} },这就是自动布局可以计算出正确的单元格高度的方式,通过这种方式,任何舍入都会显示
顺便说一句::我不知道您想要的设计,但是您必须遵循从上到下的约束约束,不仅要显示舍入,而且更重要的是要显示所需的视图将舍入应用于
答案 1 :(得分:1)
用于添加CAShapeLayer作为视图的蒙版层的代码非常接近。这行:
shapeLayer.frame = bottomDetailsView.frame
应阅读
shapeLayer.frame = bottomDetailsView.bounds
视图/图层的框架以其超级视图的坐标系表示。视图的边界定义了它的坐标系。
我没有尝试过复杂的表视图案例,但是我确实创建了UIView的子类,该子类创建了一个底部取整的掩码:
class RoundedBottomCornersView: UIView {
var shapeLayer: CAShapeLayer!
//If the view's bounds change, update the shapeLayer's frame and also rebuild the path
override var bounds: CGRect {
didSet {
shapeLayer.frame = self.bounds
createShapeLayerPath()
}
}
func createShapeLayerPath() {
let path = UIBezierPath(roundedRect: bounds,
byRoundingCorners: [.bottomLeft, .bottomRight],
cornerRadii: CGSize(width: 15, height: 15))
shapeLayer.path = path.cgPath
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
layer.borderWidth = 1.0
shapeLayer = CAShapeLayer()
shapeLayer.frame = bounds
layer.backgroundColor = UIColor.red.cgColor
layer.mask = shapeLayer
}
}