如何在UICollectionView Swift4 +中的圆周围创建实心矩形边框

时间:2020-07-21 09:27:00

标签: ios swift swift4

我尝试了以下操作,但仅在圆周围创建边框,而没有单独的矩形边框。 as shown here

cell.layer.borderWidth = 1.0
cell.layer.borderColor = UIColor.black.cgColor 

I want to create a rectangle border as shown here

2 个答案:

答案 0 :(得分:0)

代替

cell.layer.borderWidth = 1.0
cell.layer.cornerRadius = 10
cell.layer.borderColor = UIColor.black.cgColor 

您尝试过吗?

cell.layer.borderWidth = 1.0
cell.layer.borderColor = UIColor.black.cgColor 

答案 1 :(得分:0)

我不确定它是否对您有用,但是您可以尝试添加一个这样的附加层:

var rect = UIView()
var circle: CALayer?
override func loadView() {

// create view with border
    rect.frame = CGRect(x: 50, y: 50, width: 300, height: 200)
    rect.layer.borderColor = UIColor.black.cgColor
    rect.layer.backgroundColor = UIColor.clear.cgColor
    rect.layer.borderWidth = 2
    // add circle layer
    let circle = CALayer()
    circle = UIColor.red.cgColor
    // add circle layer on view with drawn border
    rect.layer.addSublayer(circle)
    view.addSubview(rect)
    circle = circle
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    // layout circle on your view
    let radius = min(rect.frame.width, rect.frame.height) * 0.5
    circle?.frame = CGRect(x: rect.bounds.width / 2 - radius / 2, y: rect.bounds.height / 2 - radius / 2, width: radius, height: radius)
    circle?.cornerRadius = radius / 2
}

但这可能不是最佳选择,也许有人会为您提供更好的解决方案。

如果它对您不起作用,请提供您遇到的问题的详细信息。

相关问题