gpt4 book ai didi

ios - 圆形 UITableViewCell 仅底部或顶部角,但它不适用于 Swift iOS 10

转载 作者:行者123 更新时间:2023-11-28 14:38:22 24 4
gpt4 key购买 nike

我想像这样圆化 2 个 UITableViewCell 的顶角和底角:这就是我在 iOS 11 上得到的

What I want

但我在 iOS 10 上得到了这个:

Result rectShape.bounds = self.bounds

使用该代码:

蓝色 View 的UITableViewCell

import UIKit

class ProductDescriptionTableViewCell: UITableViewCell {

var ProductDescription: UITextView!
public var container: UIView!

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
// Set the width of the cell
self.bounds = CGRect(x: self.bounds.origin.y + 16, y: self.bounds.origin.y, width: self.bounds.size.width - 16, height: self.bounds.size.height)

super.layoutSubviews()
}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

self.backgroundColor = UIColor.blue
if #available(iOS 11.0, *) {
self.layer.masksToBounds = true
self.layer.cornerRadius = 10
self.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]
} else {
let rectShape = CAShapeLayer()
rectShape.bounds = self.bounds// CGRect(x: self.bounds.origin.x + 16, y: self.bounds.origin.y, width: self.bounds.size.width - 16, height: self.bounds.size.height)
rectShape.position = self.center
rectShape.path = UIBezierPath(roundedRect: rectShape.bounds, byRoundingCorners: [.bottomRight, .bottomLeft], cornerRadii: CGSize(width: 10, height: 10)).cgPath

self.layer.backgroundColor = UIColor.red.cgColor
//Here I'm masking the textView's layer with rectShape layer
self.layer.mask = rectShape
}
}
}

紫色 View 的UITableViewCell

import UIKit

class ProductTitleTableViewCell: UITableViewCell {

var ProductTitle: UILabel!

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
// Set the width of the cell
self.bounds = CGRect(x: self.bounds.origin.y + 16, y: self.bounds.origin.y, width: self.bounds.size.width - 16, height: self.bounds.size.height)

super.layoutSubviews()
}

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

self.backgroundColor = UIColor.purple

if #available(iOS 11.0, *) {
self.layer.masksToBounds = true
self.layer.cornerRadius = 10
self.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
} else {
let rectShape = CAShapeLayer()
rectShape.bounds = self.bounds//CGRect(x: self.bounds.origin.x + 16, y: self.bounds.origin.y, width: self.bounds.size.width - 16, height: self.bounds.size.height)
rectShape.position = self.center
rectShape.path = UIBezierPath(roundedRect: rectShape.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 10, height: 10)).cgPath

self.layer.backgroundColor = UIColor.green.cgColor
//Here I'm masking the textView's layer with rectShape layer
self.layer.mask = rectShape
}
}
}

如果我在评论中用 CGRect 替换 self.bounds 我明白了:

With rectShape.bounds = CGRect(x: self.bounds.origin.x + 16, y: self.bounds.origin.y, width: self.bounds.size.width - 16, height: self.bounds.size.height)

我尝试在 init() 的开头更改 self.bounds,但它也不起作用

我也尝试将改变角的代码放在 layoutSubviews() 中,但在这种情况下 UITableViewCell 消失了

我不想只使用一个 UITableViewCell 并绕过他的角我真的需要绕过底角和顶角以分隔 UITableViewCell

如果您知道我该如何解决,我将不胜感激谢谢你的时间

最佳答案

添加以下扩展:

func roundCorners(_ corners:UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}

然后像下面这样从布局 subview 中调用它:

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
headerView.roundCorners([UIRectCorner.topLeft, UIRectCorner.topRight], radius: kTableViewCornerRadius)
}

关于ios - 圆形 UITableViewCell 仅底部或顶部角,但它不适用于 Swift iOS 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50719513/

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