我正在尝试在tablevviewcell子类中创建一个圆形视图
我将cornerRadius设置为宽度的一半,但框架返回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)
}
答案 0 :(得分:3)
在布局视图之前,您正在进行交互。您需要子类化UITableViewCell并覆盖layoutSubviews
,如下所示:
class CustomCell: UITableViewCell {
override func layoutSubviews() {
super.layoutSubviews()
initialView.layer.cornerRadius = initialView.frame.size.width/2
}
}
答案 1 :(得分:3)
解决此问题的方法是继承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
返回view.frame.width
heightForRowAt
}:
如果您愿意,我也可以发布用于测试此代码的UITableViewController
。