如何将两个UITextView添加到UIView中?

时间:2017-04-11 17:31:23

标签: ios swift3

我想在[1 1 2 2 3 3]中添加两个UITextViewsUIView位于UIStackView。如果我设置UITextViews的宽度和高度,它会起作用。问题是我不知道如何在没有设置宽度和高度的情况下将它们平均放置在UIView内。结果应该是这样的:

enter image description here

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)
    }

0 个答案:

没有答案