我正在尝试在我的 tablevviewcell 子类中创建一个圆形 View
我将角半径设置为宽度的一半,但框架返回 0。因此角半径设置不正确。
initialView.autoPinEdgesToSuperviewEdges(with: UIEdgeInsetsMake(5.0, 5.0, 5.0, 0.0), excludingEdge: .trailing)
initialView.autoMatch(.width, to: .height, of: initialView)
initialView.layer.cornerRadius = initialView.frame.size.width/2
谢谢
override func layoutSubviews() {
super.layoutSubviews()
initialView.layer.cornerRadius = initialView.frame.size.width/2
print(initialView.frame)
}
框架仍在打印 -> (0.0, 0.0, 0.0, 0.0)
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(initialView)
contentView.addSubview(nameLabel)
setupConstraints()
}
override func layoutSubviews() {
super.layoutSubviews()
initialView.layer.cornerRadius = initialView.frame.size.width/2
print(initialView.frame)
}
最佳答案
解决这个问题的方法是继承 UITableViewCell
并将您的代码放入 layoutSubviews
:
override func layoutSubviews() {
super.layoutSubviews()
initialView.layer.cornerRadius = initialView.frame.size.width/2
}
然后将问题中的其余代码放入 init()
代码中:
func init() {
// other stuff...
setupInitialView()
}
func setupInitialView() {
initialView.autoPinEdgesToSuperviewEdges(with: UIEdgeInsetsMake(5.0, 5.0, 5.0, 0.0), excludingEdge: .trailing)
initialView.autoMatch(.width, to: .height, of: initialView)
}
我的完整(有效!)UITableViewCell
类如下所示。我没有使用 PureLayout
pod,而是使用了基本上做同样事情的扩展方法。
//
// MyTableViewCell.swift
// tableviewheader-test
//
// Created by Forest Kunecke on 3/31/17.
// Free as in free beer!
//
import UIKit
class MyTableViewCell: UITableViewCell {
private(set) lazy var initialView: UIView = {
let initialView = UIView(frame: .zero)
return initialView
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
//
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.addSubview(initialView)
setupInitialView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func setupInitialView() {
self.contentView.addConstraintsWithFormat("V:|-5-[v0]-5-|", views: initialView)
self.contentView.addConstraintsWithFormat("H:|-5-[v0]-0-|", views: initialView)
initialView.backgroundColor = UIColor.red
}
override func layoutSubviews() {
super.layoutSubviews()
self.initialView.layer.cornerRadius = frame.size.width/2
}
}
extension UIView {
func addConstraintsWithFormat(_ format: String, views: UIView...) {
var viewsDictionary = [String: UIView]()
for (index, view) in views.enumerated() {
let key = "v\(index)"
view.translatesAutoresizingMaskIntoConstraints = false
viewsDictionary[key] = view
}
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
}
}
这是一个屏幕截图(UITableViewController
为 heightForRowAt
返回 view.frame.width
):
如果你愿意,我也可以发布我用来测试这段代码的UITableViewController
。
关于ios - UITableViewCell 子类中 View 的角半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43146000/