gpt4 book ai didi

ios - 以编程方式使用堆栈 View

转载 作者:行者123 更新时间:2023-11-28 08:22:58 25 4
gpt4 key购买 nike

我已经创建了一个自定义的 UIView 并在 View 内部尝试使用 UIStackView 添加一些 UI 元素。

在 IB 内部,我添加了一个 View ,然后将自定义类设置为我的自定义 UIView。

UIStackView 似乎不受限于我将其添加到的 UIView,而是 UIStackView 似乎锚定到原点。此外,我只能在调试 View 层次结构中看到它。它实际上并没有显示任何标签或按钮,我只看到一个虚线的灰色框。 enter image description here

enter image description here

这是我创建的自定义 View

import UIKit

class ParameterControl: UIView {

private var name : UILabel!
private var valTf : UITextField!
private var delta : UITextField!
private var stepper : UIStepper!
private var button : UIButton!

init(frame: CGRect, albumCover: String) {
super.init(frame: frame)
commonInit()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}

func commonInit(){

name = UILabel()
name.text = "Name"
name.backgroundColor = UIColor.blue

valTf = UITextField.init()
delta = UITextField.init()
stepper = UIStepper.init()
button = UIButton.init()

let stackView = UIStackView(arrangedSubviews: [valTf, delta, stepper, button])
stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.alignment = .fill
stackView.spacing = 10
stackView.translatesAutoresizingMaskIntoConstraints = false

addSubview(stackView)

//let viewsDictionary = ["stackView":stackView]
//let stackView_H = NSLayoutConstraint.constraints(withVisualFormat: "H:|-20-[stackView]-20-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary)
//let stackView_V = NSLayoutConstraint.constraints(withVisualFormat: "V:|-30-[stackView]-30-|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: viewsDictionary)
//self.addConstraints(stackView_H)
//self.addConstraints(stackView_V)

}

}

我是否错误地使用了 UIStackView?目标是能够创建这些 View 的数组,一个用于我的每个参数。我不想在 IB 中单独创建这些 View 。

最佳答案

取消注释代码中的约束并确保它们在堆栈 View 中被激活。它停留在它的默认位置和框架中,因为你没有给它任何告诉它以其他方式表现的约束。此外,请确保您的所有元素都具有有效的框架。

关于ios - 以编程方式使用堆栈 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40814426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com