Ios 角半径问题
我有以下代码Ios 角半径问题,ios,swift,Ios,Swift,我有以下代码 class CourseItemCollectionViewCell: UICollectionViewCell { var imageView: UIImageView! var label: UILabel! override init(frame: CGRect) { super.init(frame: frame) imageView = UIImageView() imag
class CourseItemCollectionViewCell: UICollectionViewCell {
var imageView: UIImageView!
var label: UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
imageView = UIImageView()
imageView.contentMode = .scaleAspectFill
imageView.isUserInteractionEnabled = false
contentView.addSubview(imageView)
}
override func layoutSubviews() {
super.layoutSubviews()
var frame = imageView.frame
frame.size.height = 120
frame.size.width = 212
frame.origin.x = 0
frame.origin.y = 0
imageView.frame = frame
imageView.layoutIfNeeded()
imageView.layer.masksToBounds = true
imageView.layer.cornerRadius = 8
不幸的是,角半径在图像视图中不起作用。结果是图像的上边缘看起来是矩形的,而下边缘看起来是圆形的。它适用于底部,但不适用于顶部。我做错了什么
我尝试过的更新:
let imageView: UIImageView = {
let imageView = UIImageView(frame: .zero)
imageView.contentMode = .scaleAspectFill
return imageView
}()
override init(frame: CGRect) {
super.init(frame: .zero)
setupViews()
setupLayouts()
}
private func setupViews() {
contentView.clipsToBounds = true
contentView.backgroundColor = .clear
contentView.isUserInteractionEnabled = true
imageView.layer.masksToBounds = true
imageView.setRoundedEdge(corners: [.topLeft, .topRight, .bottomLeft, .bottomRight], radius: 8)
contentView.addSubview(imageView)
contentView.addSubview(label)
}
extension UIImageView {
func setRoundedEdge(corners:UIRectCorner, radius: CGFloat){
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
在我看到之前
现在我明白了
注意:图像是通过URL加载的。试试这个,一些视图可能会剪裁图层并延伸到指定的帧之外。通常,当转弯半径不起作用时,我会使用此扩展: 扩展UIImageView{ func setRoundedEdge(角点:UIRectCorner,半径:CGFloat){ 让路径=UIBezierPath(roundedRect:bounds,byRoundingCorners:corners,cornerRadii:CGSize(宽度:半径,高度:半径)) let mask=CAShapeLayer() mask.path=path.cgPath 图层掩码=掩码 } } 用法为
imageView.setRoundedEdge(角点:[.topLeft、.topRight、.bottomLeft、.bottomRight],半径:8);
让我知道这是否解决了您的问题。尝试在collectionViewCell中添加containerView,在其中添加imageView(我设置我的图像,您设置url图像),并使用自动布局设置约束,如下所示:
let containerView: UIView = {
let v = UIView()
v.backgroundColor = .red
v.layer.cornerRadius = 12
v.layer.masksToBounds = true
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()
现在设置imageView和标签:
let dummyImageView: UIImageView = {
let imageView = UIImageView()
imageView.image = UIImage(named: "yourImage")
imageView.contentMode = .scaleAspectFill
imageView.layer.cornerRadius = 8
imageView.clipsToBounds = true
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
let dummyLabel: UILabel = {
let label = UILabel()
label.backgroundColor = .clear
label.isUserInteractionEnabled = false
label.text = "Lean Methodology"
label.font = UIFont.systemFont(ofSize: 30, weight: .regular)
label.textColor = .black
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
之后,使用自动布局设置约束
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
backgroundColor = .white
contentView.addSubview(containerView)
containerView.topAnchor.constraint(equalTo: topAnchor, constant: 10).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 230).isActive = true
containerView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10).isActive = true
containerView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10).isActive = true
containerView.addSubview(dummyImageView)
dummyImageView.topAnchor.constraint(equalTo: topAnchor).isActive = true
dummyImageView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
dummyImageView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
dummyImageView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
addSubview(dummyLabel)
dummyLabel.topAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
dummyLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
dummyLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
dummyLabel.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
}
结果是:
您的imageview是否仍被屏蔽到边界?imageview.LayoutIfNeed()imageview.layer.masksToBounds=true imageview.setRoundedEdge(角点:[.topLeft、.topRight、.bottomLeft、.bottomRight],半径:8);你能用之前和之后的图像更新你的问题吗?请参见问题尝试摆脱
imageView.layoutifneedd()
由此,你可以看到函数更改了边界。对于更新的部分,当你调用setRoundedEdge时,imageView的边界是(0,0,0,0)这就是图像消失的原因。然而,我仍在试图弄清楚为什么imageView的上角看起来是矩形的,因为我在尝试代码时发现所有的角看起来都是圆形的。也许您应该尝试将collectionViewCell的背景色设置为其他颜色,以查看图像是否具有白色上边框。。。?(抱歉,我知道这听起来很傻,但我真的很想看看图像的实际大小)