ios - UILabel 没有在 UIStackView 中使用 addArrangedView 呈现;使用 addSubView 渲染
问题描述
我有一个 UIStackView,我正在尝试添加 2 个标签作为子标签。当我使用 addArrangedSubview 添加它们时,我的视图内部没有任何内容呈现,但是当我只使用 addSubview 时,两个标签都会呈现(尽管不是我想要的位置)。我究竟做错了什么?
import UIKit
class RepCounterView : UIView {
var count : Int = 0
lazy var repCountLabel: UILabel = {
let repCountLabel = UILabel(frame: CGRect.init(x: 0, y: 0, width: 120, height: 120))
repCountLabel.textColor = UIColor.white
repCountLabel.font = UIFont.systemFont(ofSize: 100)
repCountLabel.text = String(count)
repCountLabel.textAlignment = .center
return repCountLabel
}()
lazy var repsLabel: UILabel = {
let repsLabel = UILabel(frame: CGRect.init(x: 0, y: 0, width: 120, height: 120))
repsLabel.textColor = UIColor.white
repsLabel.font = UIFont.systemFont(ofSize: 10)
repsLabel.text = repCountLabel.text == "1" ? "rep" : "reps"
return repsLabel
}()
lazy var stackView : UIStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.distribution = .fill
stackView.alignment = .center
return stackView
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setup()
}
override func draw(_ rect: CGRect) {
self.layer.cornerRadius = self.bounds.size.width / 2
}
func setup() {
self.clipsToBounds = true
self.addSubview(self.stackView)
// This is where .addSubview works, but this doesn't
self.stackView.addArrangedSubview(repCountLabel)
self.stackView.addArrangedSubview(repsLabel)
}
}
解决方案
import UIKit
class RepCounterView : UIView {
var count : Int = 5
lazy var repCountLabel: UILabel = {
let repCountLabel = UILabel(frame: CGRect.init(x: 0, y: 0, width: 120, height: 120))
repCountLabel.textColor = UIColor.white
repCountLabel.font = UIFont.systemFont(ofSize: 100)
repCountLabel.text = String(count)
repCountLabel.textAlignment = .center
return repCountLabel
}()
lazy var repsLabel: UILabel = {
let repsLabel = UILabel(frame: CGRect.init(x: 0, y: 0, width: 120, height: 120))
repsLabel.textColor = UIColor.white
repsLabel.font = UIFont.systemFont(ofSize: 10)
repsLabel.text = repCountLabel.text == "1" ? "rep" : "reps"
return repsLabel
}()
lazy var stackView : UIStackView = {
let stackView = UIStackView(arrangedSubviews: [repCountLabel, repsLabel])
stackView.axis = .horizontal
stackView.distribution = .fill
stackView.alignment = .center
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setup()
}
override func draw(_ rect: CGRect) {
self.layer.cornerRadius = self.bounds.size.width / 2
}
func setup() {
self.clipsToBounds = true
self.addSubview(self.stackView)
stackView.heightAnchor.constraint(equalToConstant: 200).isActive = true
stackView.widthAnchor.constraint(equalToConstant: 200).isActive = true
// This is where .addSubview works, but this doesn't
// self.stackView.addArrangedSubview(repCountLabel)
// self.stackView.addArrangedSubview(repsLabel)
}
}
推荐阅读
- eclipse - IBM MobileFirst-Eclipse 中的 JSDT UI 错误
- mysql - 使用 AJAX 获取特定单元格的值
- python-3.x - 为什么supervisor 3.3仍然设置python2.7
- r - Predict() 不会给出混合效应逻辑回归的标准误差
- android - 如何删除旧的通知渠道?
- spring - 休息模板中具有 307 状态代码的空响应实体
- python - 无法使用 boto3 检查和创建 aws 安全组
- php - Codeigniter 会话未保存在生产服务器上
- pandas - Python:如何合并两个值不唯一的数据框
- javascript - 为什么有些网页可以直接打开弹窗,而有些网页需要用户确认才能打开?(谷歌浏览器)