【问题标题】:How to create a solid rectangle border around the circle in UICollectionView Swift4+如何在 UICollectionView Swift4+ 中围绕圆圈创建实心矩形边框
【发布时间】:2020-07-21 09:27:00
【问题描述】:

我尝试了以下方法,但只在圆圈周围创建了一个边框,而不是一个单独的矩形边框。 as shown here

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

I want to create a rectangle border as shown here

【问题讨论】:

  • 你能附上一张你目前拥有的图片吗?很难仅针对这三行代码提供建议。 “圆圈周围的边界”是什么意思?您如何将圆圈添加到您的单元格中?单元格相对于圆有多大?
  • @jrturton 包含图片,感谢您的提醒。
  • 我认为您仍然需要提供更多代码。在不知道如何创建单元格或添加圆圈的情况下,很难知道发生了什么。对于标准单元格,添加边框将使其成为矩形,因此单元格的层发生了一些事情,阻止了它。圆圈是从哪里来的?

标签: ios swift swift4


【解决方案1】:

代替

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 

【讨论】:

  • 糟糕,我在调试时不小心添加了第二行。是的,我在没有第二行的情况下尝试了以下操作,它只给了我一个圆圈周围的边框,如图所示。
  • @ctrlc_ctrlv_backspace 希望它能解决你的问题
【解决方案2】:

我不确定它是否适合你,但你可以尝试像这样添加一个额外的层:

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
}

但这可能不是最好的选择,也许有人会给你更好的解决方案。

如果它不适合您,请详细说明您的问题。

【讨论】:

    猜你喜欢
    • 2016-08-17
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多