我有一个带有名为 blueView 的 subview 的自定义 TableViewCell。我想将 blueView 的宽度设置为 TableView 的宽度。如何确定 TableView 的框架?
class TableCell: UITableViewCell {
let blueView = UIView()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
blueView.frame = CGRectMake(0, 0, [width of tableView], 10)
blueView.backgroundColor = UIColor.blueColor()
contentView.addSubview(blueView)
}
}
最佳答案
你不能。此时单元格根本没有框架。
您有两个选择。
- 切换到自动布局
覆盖
layoutSubviews()
例如
override func layoutSubviews() { super.layoutSubviews() let width = CGRectGetWidth(frame) let someFrame = CGRect(x: 0, y: 0, width: width, height: 10) someView.frame = someFrame }
关于ios - Swift:初始化自定义 UITableViewCell 时获取 UITableView 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28143984/