首页 > 解决方案 > 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)
    }
}

标签: iosswiftuikituistackview

解决方案


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

试试你的这个更新的代码。 在此处输入图像描述


推荐阅读