以编程方式将视图添加到StackView中不执行任何操作

时间:2018-07-06 00:14:14

标签: swift uistackview

我有两个垂直的堆栈视图,一个以下列方式嵌套在另一个视图中:

StackView #1
   UITextField
   StackView #2 (nothing in this one)
   UIButton  

我试图以编程方式将UITextFields插入我的StackView#2,但是当我这样做时,什么也没发生。我看不到插入任何视图,屏幕上也没有任何反应。

StackView#1设置为“对齐填充”,“按比例分配”。我的StackView#2都是Fill。

当我尝试添加到StackView#2时,我没有收到约束警告,所以我很好奇为什么什么也没发生。这是我的添加方式:

let textField = UITextField(frame: CGRect(x: 0, y: 0, width: 285, height: 30))
textField.placeholder = "Name"
subtasksStackView.addArrangedSubview(textField)

1 个答案:

答案 0 :(得分:1)

以下是一些您可以添加到Playground的代码。它包含的结构与您问题中的结构相同,但是有细微的差别;我还使用了第三个堆栈视图,该堆栈视图仅用于包装所有内容,这样我就可以将内容放置在视图控制器的顶部,而不会对包含的元素进行任何不必要的调整大小(拉伸,收缩)。我怀疑这可能是一个约束问题,但这应该可以帮助您将当前的实现与可行的解决方案进行比较。

import UIKit
import PlaygroundSupport


class ViewController: UIViewController {

    // MARK: - Properties

    let mainStackView = UIStackView()
    let stackViewOne = UIStackView()
    let textFieldOne = UITextField()
    let stackViewTwo = UIStackView()
    let buttonOne = UIButton(type: .system)

    // MARK: - Lifecycle

    override func viewDidLoad() {
        view.backgroundColor = .white
        // mainStackView is used so that the UI elements can be displayed at the top of the stack view without being stretched to fill the entire view controller.
        mainStackView.axis = .horizontal
        mainStackView.alignment = .top
        mainStackView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(mainStackView)
        // mainStackView needs constraints to the view controller's view.
        mainStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        mainStackView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        mainStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        mainStackView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        // Configure the first stack view. This is StavkView#1, in your case.
        stackViewOne.axis = .vertical
        stackViewOne.alignment = .fill
        stackViewOne.distribution = .fillProportionally
        mainStackView.addArrangedSubview(stackViewOne)
        // Configure the first text field.
        textFieldOne.heightAnchor.constraint(equalToConstant: 30).isActive = true
        textFieldOne.placeholder = "Text Field One"
        textFieldOne.borderStyle = .line
        stackViewOne.addArrangedSubview(textFieldOne)
        stackViewOne.addArrangedSubview(stackViewTwo)
        // Configure the button.
        buttonOne.heightAnchor.constraint(equalToConstant: 44).isActive = true
        buttonOne.setTitle("Button", for: .normal)
        buttonOne.tintColor = .black
        stackViewOne.addArrangedSubview(buttonOne)
        // Add the second text field to the nested stack view. This is StackView#2, in your case.
        let textFieldTwo = UITextField(frame: CGRect(x: 0, y: 0, width: 285, height: 30))
        textFieldTwo.heightAnchor.constraint(equalToConstant: 30).isActive = true
        textFieldTwo.placeholder = "Name"
        textFieldTwo.borderStyle = .line
        stackViewTwo.addArrangedSubview(textFieldTwo)
    }

}

let viewController = ViewController(nibName: nil, bundle: nil)
viewController.view.frame = CGRect(x: 0, y: 0, width: 1000, height: 1000)

PlaygroundPage.current.liveView = viewController