ios - UIView 的cornerRadius 与 CAShapeLayer 不同
问题描述
我想在 a 中添加一个背景视图UITableViewCell
,并且还要将 aCAShapeLayer
作为背景视图的边框(我需要一个虚线边框)。
然而,我的问题是,即使我将视图的cornerRadius 和图层设置为相同的值,它们的呈现方式也完全不同。
有谁知道发生了什么?
编码:
final class Cell: UITableViewCell {
private let borderLayer = CAShapeLayer()
private let _backgroundView = UIView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// Add a blue background view.
contentView.insertSubview(_backgroundView, at: 0)
_backgroundView.backgroundColor = .blue
_backgroundView.layer.cornerRadius = 28
// Add the border layer.
borderLayer.fillColor = nil
borderLayer.strokeColor = UIColor.red.cgColor
borderLayer.lineWidth = 2
contentView.layer.addSublayer(borderLayer)
}
override func layoutSubviews() {
super.layoutSubviews()
// Update the frames.
_backgroundView.frame = contentView.bounds
borderLayer.path = UIBezierPath(roundedRect: contentView.bounds, cornerRadius: _backgroundView.layer.cornerRadius).cgPath
}
}
任何帮助表示赞赏!
解决方案
直接从 CGPath 构建路径似乎可以解决问题。
borderLayer.path = CGPath(roundedRect: contentView.bounds, cornerWidth: _backgroundView.layer.cornerRadius,
cornerHeight: _backgroundView.layer.cornerRadius, transform: nil)
推荐阅读
- c++ - 除而不除c ++
- wpf - 将 WPF 数据网格转换为数据表
- python - 如何使用空间数据库检查 AIS 信号是否从地理围栏发出?
- java - 如何在 Swagger 中解决这个问题 https://stackoverflow.com/questions/16015548/how-to-send-multipart-form-data-request-using-postman
- python - 如何停止打印机功能?
- android - 无法从短信广播中获取 OTP
- pdf - 在网格单元格内生成带有阿拉伯文文本的 PDF
- node.js - Webauthn - Windows Hello 身份验证器选择不起作用
- android - 华为 Unity IAP Kit 集成获取错误代码 -1,错误代码 6004
- linux - 写入一个sysfs节点,导致系统总是写入该节点