我想在[1 1 2 2 3 3]
中添加两个UITextViews
,UIView
位于UIStackView
。如果我设置UITextViews
的宽度和高度,它会起作用。问题是我不知道如何在没有设置宽度和高度的情况下将它们平均放置在UIView
内。结果应该是这样的:
class PanelCell: UICollectionViewCell {
let currentRunView: UIView = {
let view = UIView()
view.layer.cornerRadius = 5.0;
view.layer.masksToBounds = true;
view.backgroundColor = UIColor(hex: 0x1A1A1A)
return view
}()
let totalRunView: UIView = {
let view = UIView()
view.layer.cornerRadius = 5.0;
view.layer.masksToBounds = true;
view.backgroundColor = UIColor(hex: 0x1A1A1A)
return view
}()
let totalRunText: UITextView = {
let text = UITextView(x: 0, y: 0, width: 100, height: 10))
text.text = "Total run"
text.textColor = .currentSpeedColor
return text
}()
let totalRunValue: UITextView = {
let text = UITextView(frame: CGRect(x: 10, y: 10, width: 100, height: 10))
text.text = "1220 km"
text.textColor = .white
return text
}()
override init(frame: CGRect) {
super.init(frame: frame)
totalRunView.addSubview(totalRunValue)
totalRunView.addSubview(totalRunText)
let dashboardStackView = UIStackView()
dashboardStackView.axis = UILayoutConstraintAxis.horizontal
dashboardStackView.distribution = UIStackViewDistribution.fillEqually
dashboardStackView.alignment = UIStackViewAlignment.fill
dashboardStackView.spacing = 12.0
dashboardStackView.addArrangedSubview(currentRunView)
dashboardStackView.addArrangedSubview(totalRunView)
dashboardStackView.translatesAutoresizingMaskIntoConstraints = false;
addSubview(dashboardStackView)
_ = dashboardStackView.anchor(topAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: -frame.width * 0.1, leftConstant: 25, bottomConstant: 0, rightConstant: 25, widthConstant: 0, heightConstant: frame.width * 0.25)
backgroundColor = UIColor(hex: 0x212121)
}