ios - 圆形 UITableViewCell 仅底角或顶角,但它不起作用 Swift iOS 10
问题描述
我想像这样围绕 2 UITableViewCell 的顶部和底部角:这就是我在 iOS 11 上得到的
但我在 iOS 10 上得到了这个:
结果 rectShape.bounds = self.bounds
使用该代码:
蓝色视图的 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
}
}
}
紫色视图的 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 我得到了:
我尝试在 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
}
然后从布局子视图中调用它,如下所示:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
headerView.roundCorners([UIRectCorner.topLeft, UIRectCorner.topRight], radius: kTableViewCornerRadius)
}
推荐阅读
- bash - Sqlcmd:'var=1':参数无效。使用 Bash 脚本
- c - 递归 - 数据结构课程 - 打印所有可能的系列
- spring - 如何在控制器单元测试中模拟 grails 4 服务
- android - 在 NestedScrollView 中创建 RecyclerView ViewHolder
- amazon-web-services - 无服务器部署 lambda 无法承担现有的 IAM 角色
- python - 通过python将多个excel文件合并为一个excel文件
- asp.net-mvc-5 - 从 IdentityServer3 成功登录,如何自动调用 WebAPi (MVC 5 Framework 4.8),而不需要物理点击调用 API 按钮?
- ios - 驱动程序未更新为最新值
- winapi - 为什么从 Rust 调用 SystemParametersInfo 将壁纸设置为黑色?
- sql - 如果列中的值少于十个字符,则在开头添加 0