【问题标题】:Edit UIStackView programmatically以编程方式编辑 UIStackView
【发布时间】:2020-10-16 05:29:21
【问题描述】:

我目前正在尝试制作一个简单的应用程序,该应用程序将根据用户输入显示少量内容。目前,我的屏幕上有一个堆栈视图,里面有一个 UIView。我试图做到这一点,一旦按下一个按钮,UIView 就会消失,然后当另一个按钮被按下时,就会出现一个新的。该按钮可以一直被按下,更多的视图将被放入堆栈视图中。

这是我目前拥有的,它不起作用,我不知道在搜索了一段时间后如何让它工作:

import UIKit

class ViewController: UIViewController {
    
    @IBAction func deleteButtonPressed(_ button: PushButton) {
        
        @IBOutlet weak var containerStackView: UIStackView!
        containerStackView = UIStackView(arrangedSubviews: [removeIcons()])
    }

    func removeIcons() -> UIView {
        let newView = UIView(frame: CGRect(x: 0, y:0, width: 100, height: 100))
        return newView
    }
}

有没有办法引用堆栈视图然后更改它?

---- 编辑

我现在有这个代码:

class ViewController: UIViewController {
    @IBOutlet var containerStackView: UIStackView!

    @IBAction func deleteButtonPressed(_ sender: Any) {
        let newView = UIView(frame: CGRect(x: 0, y:0, width: 100, height: 100))
        newView.backgroundColor = UIColor.black
    
        containerStackView = UIStackView(arrangedSubviews: [newView])
    }
}

这似乎完美地代表了堆栈视图,但是,当我按下按钮时,堆栈视图似乎没有发生任何事情。理论上,当前存在的 UIView 应该被创建为“newView”的黑色小方块替换。虽然这似乎没有发生 - 有什么明显的我遗漏了吗?

抱歉这些问题,我是 swift 和 xcode 的新手。

【问题讨论】:

    标签: ios swift xcode uiviewcontroller uistackview


    【解决方案1】:

    首先你的出口应该在顶层:

    import UIKit
    
    class ViewController: UIViewController {
        @IBOutlet weak var containerStackView: UIStackView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
        }
    
        @IBAction func deleteButtonPressed(_ button: PushButton) {
            containerStackView = UIStackView(arrangedSubviews: [removeIcons()])
        }
    
        func removeIcons() -> UIView {
            let newView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    
            return newView
        }
    }
    

    【讨论】:

      【解决方案2】:

      在类作用域外的函数外声明出口

      class ViewController: UIViewController {
          @IBOutlet weak var containerStackView: UIStackView! // here
      
          override func viewDidLoad() {
              super.viewDidLoad()
              // Do any additional setup after loading the view.
          }
      
          @IBAction func deleteButtonPressed(_ button: PushButton) {
              containerStackView.subviews.forEach { $0.removeFromsuperView() }
          }
      
          @IBAction func daddButtonPressed(_ button: UIButton) {
              containerStackView = UIStackView(arrangedSubviews: [removeIcons()])
          }
      
          func removeIcons() -> UIView {
              let newView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
      
              return newView
          }
      }
      

      【讨论】:

        【解决方案3】:

        试试这个:

        class ViewController: UIViewController {
            //...
            @IBOutlet weak var containerStackView: UIStackView!
            //...
            @IBAction func deleteButtonPressed(_ button: PushButton) {
                let newView = UIView(frame: CGRect(x: 0, y:0, width: 100, height: 100))
                containerStackView.addArrangedSubview(newView)
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-21
          • 1970-01-01
          • 2021-05-31
          • 2021-11-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多