UILabels 不使用 addArrangedView 在 UIStackView 中呈现;使用 addSubView 渲染
UILabels not rendering in UIStackView with addArrangedView; render with 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)
}
}
试试你更新后的代码。
如果您使用自动布局提供约束,则必须添加 tackView.translatesAutoresizingMaskIntoConstraints = false
。您更新的代码,
lazy var stackView : UIStackView = {
let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.alignment = .fill
return stackView
}()
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)
stackView.widthAnchor.constraint(equalToConstant: 200).isActive = true
stackView.heightAnchor.constraint(equalToConstant: 200).isActive = true
}
我有一个 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)
}
}
试试你更新后的代码。
如果您使用自动布局提供约束,则必须添加 tackView.translatesAutoresizingMaskIntoConstraints = false
。您更新的代码,
lazy var stackView : UIStackView = {
let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.alignment = .fill
return stackView
}()
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)
stackView.widthAnchor.constraint(equalToConstant: 200).isActive = true
stackView.heightAnchor.constraint(equalToConstant: 200).isActive = true
}