以编程方式未添加到 UIStackView 的元素?

Posted

技术标签:

【中文标题】以编程方式未添加到 UIStackView 的元素?【英文标题】:Elements not adding to UIStackView , programmatically? 【发布时间】:2021-02-16 08:50:32 【问题描述】:

我试图在 Live Playground 中创建一个排序可视化工具,所以我创建了一个堆栈视图,但是当我尝试添加 UIView(作为该可视化工具中的棒)时,它们没有被添加到我的 stackView 中,下面是我的代码 它很大,但你对 mainStackview 部分感兴趣只是因为那是我想添加那个棒(UIView)的 stackView,还有 buildStartingArray() 函数(它用于添加 UIViews)

public class HomeViewController:UIViewController

    let stackView:UIStackView = 
       let st = UIStackView()
        st.axis = .horizontal
        st.alignment = .center
        st.distribution = .fill
//        st.backgroundColor = .cyan
        st.layer.shadowColor = UIColor.gray.cgColor
        st.layer.shadowOffset = .zero
        st.layer.shadowRadius = 5
        st.layer.shadowOpacity = 1
        st.spacing = 10
        st.translatesAutoresizingMaskIntoConstraints = false
        
       return st
    ()
    let generateButton:UIButton = 
       let btn = UIButton()
        btn.setTitle("Generate Array", for: .normal)
        btn.backgroundColor = UIColor(red: 0.92, green: 0.30, blue: 0.29, alpha: 1.00)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    ()
    let BubbleSort:UIButton = 
       let btn = UIButton()
        btn.setTitle("BubbleSort", for: .normal)
        btn.backgroundColor = UIColor(red: 0.41, green: 0.43, blue: 0.88, alpha: 1.00)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    ()
    let MergeSort:UIButton = 
       let btn = UIButton()
        btn.setTitle("MergeSort", for: .normal)
        btn.backgroundColor = UIColor(red: 0.10, green: 0.16, blue: 0.34, alpha: 1.00)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    ()
    let InsertionSort:UIButton = 
       let btn = UIButton()
        btn.setTitle("InsertionSort", for: .normal)
        btn.backgroundColor = UIColor(red: 0.19, green: 0.22, blue: 0.32, alpha: 1.00)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    ()
    let SelectionSort:UIButton = 
       let btn = UIButton()
        btn.setTitle("SelectionSort", for: .normal)
        btn.backgroundColor = UIColor(red: 0.51, green: 0.20, blue: 0.44, alpha: 1.00)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    ()
    let mainStackView:UIStackView = 
        let st = UIStackView()
        st.backgroundColor = .gray
        st.axis = .horizontal
        st.distribution = .fillEqually
        st.alignment = .firstBaseline
        st.spacing = 1
        st.translatesAutoresizingMaskIntoConstraints = false
        return st
    ()
    let baseView:UIView = 
        let vw = UIView()
        vw.backgroundColor = UIColor(red: 0.07, green: 0.54, blue: 0.65, alpha: 1.00)
        vw.translatesAutoresizingMaskIntoConstraints = false
        vw.layer.cornerRadius = 3
        vw.layer.masksToBounds = true
        vw.heightAnchor.constraint(equalToConstant: 15).isActive = true
        return vw
    ()
    public override   func viewDidLoad() 
          view.addSubview(mainStackView)
          view.addSubview(stackView)
          view.addSubview(baseView)
        edgesForExtendedLayout = []
        stackView.addArrangedSubview(generateButton)
        stackView.addArrangedSubview(BubbleSort)
        stackView.addArrangedSubview(MergeSort)
        stackView.addArrangedSubview(InsertionSort)
        stackView.addArrangedSubview(SelectionSort)
        
        stackView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        stackView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        stackView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        stackView.heightAnchor.constraint(equalToConstant: 50).isActive = true
        
        baseView.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: -2).isActive = true
        baseView.leftAnchor.constraint(equalTo: view.leftAnchor,constant: 5).isActive = true
        baseView.rightAnchor.constraint(equalTo: view.rightAnchor,constant: -5).isActive = true

         mainStackView.topAnchor.constraint(equalTo: stackView.bottomAnchor, constant: 5).isActive = true
        mainStackView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 5).isActive = true
        mainStackView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -5).isActive = true
        mainStackView.bottomAnchor.constraint(equalTo: baseView.topAnchor, constant: -5).isActive = true
        setButtons()
        builStartingArray()

    

   func setButtons()
        generateButton.addTarget(self, action: #selector(generatePressed), for: .touchUpInside)
        BubbleSort.addTarget(self, action: #selector(bubbleSort), for: .touchUpInside)
        MergeSort.addTarget(self, action: #selector(mergeSort), for: .touchUpInside)
        InsertionSort.addTarget(self, action: #selector(insertionSort), for: .touchUpInside)
        SelectionSort.addTarget(self, action: #selector(selectionSort), for: .touchUpInside)
    
    func builStartingArray()
        let viewStick:UIView = 
           let v = UIView()
            v.backgroundColor = .red
            v.translatesAutoresizingMaskIntoConstraints = false
            v.frame.size = CGSize(width: 50, height: 150)
            return v
        ()
        let viewStick2:UIView = 
           let v = UIView()
            v.backgroundColor = .red
            v.translatesAutoresizingMaskIntoConstraints = false
            v.frame.size = CGSize(width: 50, height: 150)
            return v
        ()
        mainStackView.addArrangedSubview(viewStick)
        mainStackView.addArrangedSubview(viewStick2)
    

问题是我在 buildStartingArray() 函数中添加的 UIView 没有显示,并且 目前我只添加了 2 个视图,但将来我计划在 mainStackView 中添加 30-35,

我希望这些 UIView 看起来像图表条,而 mainStackView 是我的图表

这是当前的输出 mainStackView 是灰色背景的

【问题讨论】:

尝试删除translatesAutoresizingMaskIntoConstraints = false并让stackview设置其内容。 添加可复现的代码或github链接 @ibrahimyilmaz ,什么视图或 mainStack ,哪些要删除?? 【参考方案1】:

使主stackView底部对齐

您可以尝试为 mainStack 子视图添加高度,例如

v.translatesAutoresizingMaskIntoConstraints = false
v.heightAnchor.constraint(equalToConstant: 150).isActive = true

func builStartingArray()
    let viewStick:UIView = 
       let v = UIView()
        v.backgroundColor = .red
        v.translatesAutoresizingMaskIntoConstraints = false
        v.heightAnchor.constraint(equalToConstant: 150).isActive = true
        return v
    ()
    let viewStick2:UIView = 
       let v = UIView()
        v.backgroundColor = .red
        v.translatesAutoresizingMaskIntoConstraints = false
        v.heightAnchor.constraint(equalToConstant: 150).isActive = true
        return v
    ()
    mainStackView.addArrangedSubview(viewStick)
    mainStackView.addArrangedSubview(viewStick2)

所有代码

import UIKit

class ViewController:UIViewController

   let stackView:UIStackView = 
       let st = UIStackView()
        st.axis = .horizontal
        st.alignment = .center
        st.distribution = .fill
//        st.backgroundColor = .cyan
        st.layer.shadowColor = UIColor.gray.cgColor
        st.layer.shadowOffset = .zero
        st.layer.shadowRadius = 5
        st.layer.shadowOpacity = 1
        st.spacing = 10
        st.translatesAutoresizingMaskIntoConstraints = false
        
       return st
    ()
    let generateButton:UIButton = 
       let btn = UIButton()
        btn.setTitle("Generate Array", for: .normal)
        btn.backgroundColor = UIColor(red: 0.92, green: 0.30, blue: 0.29, alpha: 1.00)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    ()
    let BubbleSort:UIButton = 
       let btn = UIButton()
        btn.setTitle("BubbleSort", for: .normal)
        btn.backgroundColor = UIColor(red: 0.41, green: 0.43, blue: 0.88, alpha: 1.00)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    ()
    let MergeSort:UIButton = 
       let btn = UIButton()
        btn.setTitle("MergeSort", for: .normal)
        btn.backgroundColor = UIColor(red: 0.10, green: 0.16, blue: 0.34, alpha: 1.00)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    ()
    let InsertionSort:UIButton = 
       let btn = UIButton()
        btn.setTitle("InsertionSort", for: .normal)
        btn.backgroundColor = UIColor(red: 0.19, green: 0.22, blue: 0.32, alpha: 1.00)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    ()
    let SelectionSort:UIButton = 
       let btn = UIButton()
        btn.setTitle("SelectionSort", for: .normal)
        btn.backgroundColor = UIColor(red: 0.51, green: 0.20, blue: 0.44, alpha: 1.00)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.italicSystemFont(ofSize: 20)
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        btn.heightAnchor.constraint(equalToConstant: 38).isActive = true
        btn.translatesAutoresizingMaskIntoConstraints = false
        return btn
    ()
    let mainStackView:UIStackView = 
        let st = UIStackView()
        st.backgroundColor = .gray
        st.axis = .horizontal
        st.distribution = .fillEqually
        st.alignment = .top
        st.spacing = 1
        st.translatesAutoresizingMaskIntoConstraints = false
        return st
    ()
    let baseView:UIView = 
        let vw = UIView()
        vw.backgroundColor = UIColor(red: 0.07, green: 0.54, blue: 0.65, alpha: 1.00)
        vw.translatesAutoresizingMaskIntoConstraints = false
        vw.layer.cornerRadius = 3
        vw.layer.masksToBounds = true
        return vw
    ()
    public override   func viewDidLoad() 
          view.addSubview(mainStackView)
          view.addSubview(stackView)
          view.addSubview(baseView)
        edgesForExtendedLayout = []
        stackView.addArrangedSubview(generateButton)
        stackView.addArrangedSubview(BubbleSort)
        stackView.addArrangedSubview(MergeSort)
        stackView.addArrangedSubview(InsertionSort)
        stackView.addArrangedSubview(SelectionSort)
        
        stackView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        stackView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        stackView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        stackView.heightAnchor.constraint(equalToConstant: 50).isActive = true
        
        baseView.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: -2).isActive = true
        baseView.leftAnchor.constraint(equalTo: view.leftAnchor,constant: 5).isActive = true
        baseView.rightAnchor.constraint(equalTo: view.rightAnchor,constant: -5).isActive = true
        baseView.heightAnchor.constraint(equalToConstant: 15).isActive = true
        
        mainStackView.topAnchor.constraint(equalTo: stackView.bottomAnchor, constant: 5).isActive = true
        mainStackView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 5).isActive = true
        mainStackView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -5).isActive = true
        mainStackView.bottomAnchor.constraint(equalTo: baseView.topAnchor, constant: -5).isActive = true
 
        builStartingArray()

    
 
    func builStartingArray()
        let viewStick:UIView = 
           let v = UIView()
            v.backgroundColor = .red
            v.translatesAutoresizingMaskIntoConstraints = false
            v.heightAnchor.constraint(equalToConstant: 150).isActive = true
            return v
        ()
        let viewStick2:UIView = 
           let v = UIView()
            v.backgroundColor = .red
            v.translatesAutoresizingMaskIntoConstraints = false
            v.heightAnchor.constraint(equalToConstant: 150).isActive = true
            return v
        ()
        mainStackView.addArrangedSubview(viewStick)
        mainStackView.addArrangedSubview(viewStick2)
    

【讨论】:

baseView 设置高度你有一个模棱两可的高度?你现在得到了什么? 基本视图设置了 heightAnchor ,它对基本视图有什么影响? ,问题出在 mainStackView 它确实与剩余高度有关 哦,对不起,我现在编辑了,请检查?,谢谢,baseView的代码就在viewdidload的正上方 嘿@Sh_Khan,它的工作原理,但是如果我将一个视图的高度设置为大于高度较小的视图的高度,那么它们的高度就会相同?我希望它看起来像这样camo.githubusercontent.com/…,我不想要那么多条但至少 30-40

以上是关于以编程方式未添加到 UIStackView 的元素?的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式将笔尖添加到 UIStackView

如何以编程方式将 UIImageView 添加到 UIStackView(故事板)

在 Swift 4 中以编程方式将 UIImageView、UILabel 添加到 UIStackView

如何以编程方式向 UIStackView 添加安全区域布局?

UIStackView 宽度和高度不改变以下元素添加

以编程方式将一堆自定义视图从 XIB 添加到 UIStackView