gpt4 book ai didi

swift - UITableViewCell 中的 UIView 有双边框

转载 作者:行者123 更新时间:2023-11-30 12:09:49 24 4
gpt4 key购买 nike

使用下面的代码,我添加了一个彩色边框并圆化了传递的角。但是,当我以编程方式调整 UIView 的大小时,旧边框仍然存在,并且我得到了这个双边框:

Double border on UIView within a UITableViewCell

有人知道如何修复它吗?我尝试调用 cell.layoutSubviews()cell.layer.layoutSublayers()。在设置边框之前,我尝试删除边框图层并将图层设置为nil。似乎没有任何作用。

如果我不调整 View 大小,那就没问题。

func roundCorners (view: UIView, corners: UIRectCorner, radius: CGFloat, color: String) {
let bounds = view.bounds

let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let maskLayer = CAShapeLayer()
maskLayer.frame = bounds
maskLayer.path = maskPath.cgPath

view.layer.mask = maskLayer

var borderColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0)
switch color {
case "Green":
borderColor = UIColor(red: 130/255.0, green: 208/255.0, blue: 151/255.0, alpha: 0.9)
case "Red":
borderColor = UIColor(red: 243/255.0, green: 44/255.0, blue: 31/255.0, alpha: 1.0)
view.alpha = 0.5
case "Purp":
borderColor = UIColor(red: 102/255.0, green: 135/255.0, blue: 241/255.0, alpha: 0.7)
case "DarkBlue":
borderColor = UIColor(red: 90/255.0, green: 125/255.0, blue: 160/255.0, alpha: 0.6)
default:
borderColor = UIColor(red: 150/255.0, green: 150/255.0, blue: 150/255.0, alpha: 0.7)
}

let frameLayer = CAShapeLayer()
frameLayer.frame = bounds
frameLayer.path = maskPath.cgPath
frameLayer.strokeColor = borderColor.cgColor
frameLayer.lineWidth = 3
frameLayer.fillColor = nil

view.layer.addSublayer(frameLayer)
}

cellForRowAt调用:

 roundCorners(view: cell.MoveCellTo_TodayList, corners: [.topLeft, .bottomLeft], radius: 8, color: "Green")

最佳答案

maskLayerframeLayer 移出函数的作用域,并在单元格重写的 layoutSubviews 内调用 roundCorners

var maskLayer: CAShapeLayer!
var frameLayer: CAShapeLayer!

override func layoutSubviews() {
super.layoutSubviews()

roundCorners(view: MoveCellTo_TodayList, maskLayer: &maskLayer, frameLayer: &frameLayer, corners: [.topLeft, .bottomLeft], radius: 8, color: "Green")
}

func roundCorners(view: UIView, maskLayer: inout CAShapeLayer, frameLayer: inout CAShapeLayer, corners: UIRectCorner, radius: CGFloat, color: String) {
let bounds = view.bounds

let maskPath = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
if maskLayer == nil {
maskLayer = CAShapeLayer()
view.layer.mask = maskLayer

frameLayer = CAShapeLayer()
view.layer.addSublayer(frameLayer)
}

[...]
}

关于swift - UITableViewCell 中的 UIView 有双边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46218083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com