如何将两个UITextViews添加到UIView?
我想添加两个UITextViews
到放置在UIStackView
中的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) }