gpt4 book ai didi

ios - 如何在 uitableviewcell 中快速获得一个圆形的 UIbutton?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:15:14 24 4
gpt4 key购买 nike

class CustomTableViewCell: UITableViewCell {
let nameLbl: UILabel = UILabel()
let profileBtn: UIButton = UIButton()
let aboutLbl: UILabel = UILabel()


override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

contentView.addSubview(profileBtn)
contentView.addSubview(nameLbl)
contentView.addSubview(aboutLbl)

nameLbl.translatesAutoresizingMaskIntoConstraints = false
profileBtn.translatesAutoresizingMaskIntoConstraints = false
aboutLbl.translatesAutoresizingMaskIntoConstraints = false


profileBtn.backgroundColor = UIColor.red

nameLbl.font = UIFont(name: "Arial", size: 16)
aboutLbl.font = UIFont(name: "Arial", size: 16)
profileBtn.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
profileBtn.leftAnchor.constraint(equalTo: leftAnchor, constant: 20).isActive = true
profileBtn.widthAnchor.constraint(equalToConstant: 40).isActive = true
profileBtn.heightAnchor.constraint(equalToConstant: 40).isActive = true
self.profileBtn.layer.masksToBounds = true
self.profileBtn.layer.cornerRadius = CGFloat(roundf(Float(self.profileBtn.frame.size.width/2.0)))

nameLbl.topAnchor.constraint(equalTo: topAnchor, constant: 30).isActive = true
nameLbl.leftAnchor.constraint(equalTo: leftAnchor, constant: 70).isActive = true
nameLbl.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true

aboutLbl.topAnchor.constraint(equalTo: nameLbl.bottomAnchor, constant: 10).isActive = true
aboutLbl.leftAnchor.constraint(equalTo: leftAnchor, constant: 70).isActive = true
aboutLbl.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true


}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

我希望单元格内的配置文件按钮具有圆形设计。但是即使将 corener radius 和 marskstobounds 设置为 true,我也会得到一个方形按钮。我做错了什么任何帮助都是值得赞赏的。提前致谢。

最佳答案

您在尚未布置配置文件按钮时计算拐角半径。这意味着配置文件按钮的宽度将为零,呈现相同的角半径。将您设置圆角半径的行移动到 layoutSubviews 的重写方法 - 这将确保 View 和后续尺寸已经布置好,以便您设置适当的圆角半径。

override func layoutSubviews() {

super.layoutSubviews()

profileBtn.layer.cornerRadius = profileBtn.frame.width / 2
}

关于ios - 如何在 uitableviewcell 中快速获得一个圆形的 UIbutton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52758342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com