ios - 为什么在我的自定义 UIView 类中没有调用 init(frame: CGRect)?
问题描述
我有一个UIView
被调用的子类,GradientView
我在UITableViewCell
layoutSubviews()
被调用,但我的init
方法从未被调用。
import UIKit
class GradientView: UIView {
var gradientLayer = CAGradientLayer()
override init(frame: CGRect) {
super.init(frame:frame)
self.backgroundColor = .orange
gradientLayer.colors = [UIColor.clear.cgColor, UIColor.black.cgColor]
gradientLayer.locations = [0.6, 1.0]
gradientLayer.frame = self.bounds
self.layer.insertSublayer(gradientLayer, at: 0)
}
override func layoutSubviews() {
super.layoutSubviews()
self.gradientLayer.frame = self.bounds
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
我错过了什么?
解决方案
UITableViewCell
通常不使用 初始化init(frame:)
,而是使用init(style: UITableViewCellStyle, reuseIdentifier: String?)
或初始化required init?(coder aDecoder: NSCoder)
。
第一个将在您使用表视图注册类时使用,第二个将在从情节提要初始化或使用表视图注册 xib 文件时使用。
推荐阅读
- javascript - Javascript 循环只运行一次
- magento2 - Magento 2:是否可以将扩展名从 html 切换到 phtml
- python - 在带有置信区间的python中绘制偏自相关和互相关图
- sql-server - SQL Server 中关键字“DEFAULT”附近的语法不正确
- ios - 在ios swift中根据“id”参数获取不同的值
- sql - 为什么 AES_ENCRYPT 在 SQL 服务器上不起作用
- aws-lambda - 使用 CF 模板创建 AWS::Pinpoint::PushTemplate
- qt - 在 RowLayout 中使用其他元素标记自动换行
- mongodb - 如何使用 Mongoose 更新嵌套数组?
- java - java类外的访问接口