ios - UITableViewCell 子类中 View 的角半径

标签 ios swift frame cornerradius

我正在尝试在我的 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))
    }
}

这是一个屏幕截图(UITableViewControllerheightForRowAt 返回 view.frame.width):

enter image description here

如果你愿意,我也可以发布我用来测试这段代码的UITableViewController

关于ios - UITableViewCell 子类中 View 的角半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43146000/

相关文章:

html - 如何使用框架集从主页菜单定向到第二个主页?

java - 如何关闭带有线程的java框架

ios - iOS 上的 NSInteger 与 64 位架构

android - 即使应用程序关闭, react native 中的位置跟踪?

ios - 设置 persistenceStoreDescriptions 会导致不保存 CoreData 对象

ios - swift/ios 在后台刷新应用程序数据

ios - self.view 有限制吗?

ios - 类 'ViewController' 没有初始化程序,导致 XCode 崩溃

ios - 实现服务器

ios - Swift:如何在 Sprite 上设置水平前后移动?