为什么第一个单元格与我的表观视图的第二个单元格之间存在表观视图差距? (仅发生在iPhone 11上)

发布于 2025-02-05 05:57:11 字数 1221 浏览 3 评论 0原文

我想创建一个带有圆角(例如Apple Music App)的顶部的桌面视图。因此,我的解决方案是如下代码中对第一个单元格应用修改。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell
    else {
        fatalError("The dequeued cell is not an instance of Pack.")
    }
    
    if indexPath.row == 0{
        cell.roundCorners([.topLeft,.topRight], radius: cR)
    }

    return cell
}

extension UIView {
    func roundCorners(_ 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
    }
}

我在许多设备上测试了解决方案,并且效果很好,但是当我在iPhone 11上尝试时,第一和第二个单元格之间出现了一个小的1px差距。有人知道为什么吗? iPhone 11有什么特别之处吗?可能是由于不良的习惯,以这种方式绕弯头?

I wanted to create a tableView with a top having rounded corners (like Apple Music app). So my solution was to apply a modification to the first cell as in the code below.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as? CustomCell
    else {
        fatalError("The dequeued cell is not an instance of Pack.")
    }
    
    if indexPath.row == 0{
        cell.roundCorners([.topLeft,.topRight], radius: cR)
    }

    return cell
}

extension UIView {
    func roundCorners(_ 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
    }
}

I tested the solution on many devices and it worked well, but when I tried on iPhone 11, there was a small 1px gap appearing between the first and second cell. Does anyone knows why? Is there something special with iPhone 11? Could it be due to a bad practice to round corners this way?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗梦空间 2025-02-12 05:57:12

从ios11开始,有一种新的圆角方法,这不会使故障:

layer.cornerRadius = radius
            layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]

there is a newer way to round corners starting from ios11 that doesn't make the glitch:

layer.cornerRadius = radius
            layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文