在自定义tableView单元格中,我想在顶部创建带圆角的UIView . 这是我设置UIView约束的代码部分
headerView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 8).isActive = true
headerView.topAnchor.constraint(equalTo: self.topAnchor, constant: 4).isActive = true
headerView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -8).isActive = true
headerView.heightAnchor.constraint(equalToConstant: 40).isActive = true
这部分的波纹管设置圆角视图我做了以下工作:
self.layoutIfNeeded()
let rectShape = CAShapeLayer()
rectShape.bounds = headerView.layer.frame
rectShape.position = headerView.center
rectShape.path = UIBezierPath(roundedRect: headerView.layer.bounds, byRoundingCorners: [ .topLeft, .topRight], cornerRadii: CGSize(width: 10, height: 10)).cgPath
headerView.layer.backgroundColor = UIColor.green.cgColor
headerView.layer.mask = rectShape
问题是当这段代码用于设置圆角时,视图的大小是变化的 . 这是输出
没有这部分代码的结果如下:
我的问题是为什么视图大小正在改变?我做错了什么?
2 回答
这是因为一旦调整了Header View的约束,你的CAShapeLayer就不会自动调整大小 . 每当调整视图大小时,您都需要更新路径:
我以awakeFromNib为例 . 您的自定义UITableViewCell类的视图设置可能有所不同 .
可能,在调整视图大小以适应表之前,您将获得headerView边界 .
如果在子类UIView中,你应该这样做