简体   繁体   English

向 stackView 添加标签时,将添加 4 个而不是 5 个标签。Swift

[英]When adding labels to the stackView, instead of 5 labels, it will add 4. Swift

I need labels to appear on the screen depending on the number of objects in the symbols array, there are 5 objects in the array, and 4 labels on the screen(as in the screenshot), what is the problem?我需要根据符号数组中的对象数量在屏幕上显示标签,数组中有 5 个对象,屏幕上有 4 个标签(如屏幕截图所示),有什么问题? The code is attached below代码附在下面

class ViewController: UIViewController {

    var symbols = ["1", "2","3", "4", "5"]
    
    let mySpacing: CGFloat = 5.0

    let widthLabelInFormula = 50

    let formulaStackView = UIStackView()

    var symbolLabels: [UILabel] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let widthFormula: CGFloat = CGFloat(50 * symbols.count - symbols.count * 50)
        formulaStackView.axis = .horizontal
        formulaStackView.alignment = .fill
        formulaStackView.distribution = .fillEqually
        formulaStackView.spacing = mySpacing
        formulaStackView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        view.addSubview(formulaStackView)
    
        // add constraints
        formulaStackView.translatesAutoresizingMaskIntoConstraints = false
        formulaStackView.heightAnchor.constraint(equalToConstant: 50).isActive = true
        formulaStackView.widthAnchor.constraint(equalToConstant: widthFormula).isActive = true
        formulaStackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 250).isActive = true
    
        
        addLabels(formulaStackView: formulaStackView)
    }

    func addLabels(formulaStackView: UIStackView) {
        for _ in 0 ..< symbols.count {
            addLabel(formulaStackView: formulaStackView)
        }
        print(symbolLabels.count) // 5
    }

    func addLabel(formulaStackView: UIStackView) {
        let symbolLabel = UILabel()
        symbolLabel.backgroundColor = #colorLiteral(red: 0.09019608051, green: 0, blue: 0.3019607961, alpha: 1)
        symbolLabel.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        symbolLabel.textAlignment = .center
        symbolLabel.font = UIFont.systemFont(ofSize: 20)
        symbolLabel.text = "?"
        
        // add constraints
        symbolLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true
        symbolLabel.widthAnchor.constraint(equalToConstant: 50).isActive = true
        symbolLabel.translatesAutoresizingMaskIntoConstraints = false
    
        
        symbolLabels.append(symbolLabel)
        formulaStackView.addArrangedSubview(symbolLabel)
    }
}

图片

The problem here is that you are setting the stack view's width to zero in this line:这里的问题是您在此行中将堆栈视图的宽度设置为零:

let widthFormula: CGFloat = CGFloat(50 * symbols.count - symbols.count * 50) // 0

You can actually delete the line above and also this line:您实际上可以删除上面的行和这一行:

formulaStackView.widthAnchor.constraint(equalToConstant: widthFormula).isActive = true

Because UIKit can set it automatically.因为 UIKit 可以自动设置。

Finished code完成代码

class ViewController: UIViewController {
    var symbols = ["1", "2","3", "4", "5"]

    let mySpacing: CGFloat = 5.0

    let widthLabelInFormula = 50

    let formulaStackView = UIStackView()

    var symbolLabels: [UILabel] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
//        let widthFormula: CGFloat = CGFloat(50 * symbols.count - symbols.count * 50)
        formulaStackView.axis = .horizontal
        formulaStackView.alignment = .fill
        formulaStackView.distribution = .fillEqually
        formulaStackView.spacing = mySpacing
        formulaStackView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        view.addSubview(formulaStackView)

        // add constraints
        formulaStackView.translatesAutoresizingMaskIntoConstraints = false
        formulaStackView.heightAnchor.constraint(equalToConstant: 50).isActive = true
//        formulaStackView.widthAnchor.constraint(equalToConstant: widthFormula).isActive = true
        formulaStackView.topAnchor.constraint(equalTo: view.topAnchor, constant: 250).isActive = true

        
        addLabels(formulaStackView: formulaStackView)
    }

    func addLabels(formulaStackView: UIStackView) {
        for _ in 0 ..< symbols.count {
            addLabel(formulaStackView: formulaStackView)
        }
        print(symbolLabels.count) // 5
    }

    func addLabel(formulaStackView: UIStackView) {
        let symbolLabel = UILabel()
        symbolLabel.translatesAutoresizingMaskIntoConstraints = false
        symbolLabel.backgroundColor = #colorLiteral(red: 0.09019608051, green: 0, blue: 0.3019607961, alpha: 1)
        symbolLabel.textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        symbolLabel.textAlignment = .center
        symbolLabel.font = UIFont.systemFont(ofSize: 20)
        symbolLabel.text = "?"
        
        // add constraints
        symbolLabel.heightAnchor.constraint(equalToConstant: 50).isActive = true
        symbolLabel.widthAnchor.constraint(equalToConstant: 50).isActive = true

        
        symbolLabels.append(symbolLabel)
        formulaStackView.addArrangedSubview(symbolLabel)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在swift中为包含在stackview中的标签提供动态高度 - How to give dynamic height to labels contained withing stackview in swift 通过在 Swift 中添加两个标签而不是标题来自定义导航栏 - Customize navigation bar by adding two labels instead of title in Swift 使用等宽标签为水平 StackView 添加分隔符视图 - Adding Separator View for Horizontal StackView with Equal Width Labels 快速以编程方式添加不同数量的标签和滑块 - Adding varying numbers of labels and sliders programmatically in swift 如何对堆栈视图中的所有标签应用相同的大小? - How to apply same for size for all labels in a stackview? 具有两个表视图和标签的Stackview导致重叠 - Stackview with two table views and labels causes overlap 如何添加两个标签作为空的UITableView背景,而不是迅速添加一个标签? - How can I add two labels as an empty UITableView background instead of one in swift? 在Swift的UIButton内是否可以添加两个标签? - Is it possible to add two labels inside an UIButton on Swift? 在ScrollView Xcode Swift内部添加标签 - Add labels inside of ScrollView Xcode Swift 在水平StackView内,将标签拉伸到较小的屏幕时,这些标签为切边。 如何解决呢? - Inside the horizontal StackView the labels are cutoff edges when it stretched to smaller screens. How to solve this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM