我有这个UITableViewCell
class MyCell: UITableViewCell {
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var firstPartView: UIView!
override func awakeFromNib() {
super.awakeFromNib()
//container view corner radius
self.containerView.layer.cornerRadius = 10.0
self.containerView.layer.masksToBounds = true
self.containerView.layer.isOpaque = false
//shadow
self.containerView.layer.shadowColor = UIColor.black.cgColor
self.containerView.layer.shadowOffset = CGSize(width: 0, height: 1.0)
self.containerView.layer.shadowOpacity = 0.2
self.containerView.layer.shadowRadius = 4.0
//gradient color
self.firstPartView.setGradientBackground(startColor: UIColor(red: 0.224, green: 0.592, blue: 1.0, alpha: 1.0), endColor: UIColor(red: 0.553, green: 0.765, blue: 1.0, alpha: 1.0))
//rounded corners (only topLeft and bottomLeft)
self.firstPartView.roundCorners(corners: [.topLeft, .bottomLeft], radius: 10.0)
}
}
roundCorners
函数在哪里:
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
FirstPartView是容器视图左侧的一个简单视图,如下所示:
但这是最终结果:
如您所见,firstPartView的高度不正确。
读到此错误,我发现在awakeFromNib
函数中尚未绘制rects,因此我尝试从roundCorners
删除awakeFromNib
调用,并创建了一个新的UIView类,如下所示:
class RoundedCornerFix: UIView {
override func layoutSubviews() {
super.layoutSubviews()
self.roundCorners(corners: [.topLeft, .bottomLeft], radius: 10.0)
}
}
很不幸,没有运气。结果是:
我也做了这个改变:
override func awakeFromNib() {
super.awakeFromNib()
[...]
//rounded corners (only topLeft and bottomLeft)
self.firstPartView.layoutIfNeeded()
self.firstPartView.roundCorners(corners: [.topLeft, .bottomLeft], radius: 10.0)
}
但是问题是相同的