Swift 将视图动态添加到视图中
我正在尝试向Swift 将视图动态添加到视图中,swift,autolayout,uistackview,Swift,Autolayout,Uistackview,我正在尝试向UIStackView动态添加视图(或按钮) 首先,UIStackView没有排列视图(垂直),并且 从某个http响应获取后,会将多个视图(按钮)添加到UIStackView。 UIStackView也是自动布局以容纳特定区域。 我试图找到动态添加示例,但失败了 任何人都可以向我展示在UIStackView上动态添加视图的示例?我在我的一个项目中使用了此代码: let baseFrame = CGRect(origin: .zero, size: CGSize(width:
UIStackView
动态添加视图(或按钮)
首先,UIStackView
没有排列视图(垂直),并且
从某个http响应获取后,会将多个视图(按钮)添加到UIStackView
。
UIStackView
也是自动布局以容纳特定区域。
我试图找到动态添加示例,但失败了
任何人都可以向我展示在
UIStackView
上动态添加视图的示例?我在我的一个项目中使用了此代码:
let baseFrame = CGRect(origin: .zero, size: CGSize(width: requiredWidth, height: partitionHeight))
for instrument in instruments {
let partitionView = PartitionOnDemand(instrument: instrument, mode: playbackMode, frame: baseFrame, referenceView: partitionsAnimator)
partitionsStackView.addArrangedSubview(partitionView)
let tab = InstrumentInfoTabContainer.instantiate(with: instrument) {
self.focus(on: instrument)
}
tabsStackView.addArrangedSubview(tab)
}
这可能对你有帮助。请遵循以下几点:
- 将
添加到情节提要或XIB中的UIScrollView
UIViewController
- 启动一个
命名它NSMutableArray
获取服务器响应并在数组中添加视图arrViews
- 在init方法中初始化
passUIStackView
array李>arrView
- 之后,
将添加到UIStackView
的子视图中UIScrollView
- 以编程方式将约束添加到
。就这样UIStackView
if let response = self.serverResponse { if let body = response.responseBody { if let view = body.views { arrViews = createSubViews(view) } } } let stackView = UIStackView(arrangedSubviews: arrViews) stackView.translatesAutoresizingMaskIntoConstraints = false stackView.axis = .vertical stackView.spacing = 16 stackView.distribution = .fill self.scrollView.addSubview(stackView) //constraints let leading = NSLayoutConstraint(item: stackView, attribute: .leading, relatedBy: .equal, toItem: self.scrollView, attribute: .leading, multiplier: 1.0, constant: 0) self.scrollView.addConstraint(leading) let trailing = NSLayoutConstraint(item: stackView, attribute: .trailing, relatedBy: .equal, toItem: self.scrollView, attribute: .trailing, multiplier: 1.0, constant: 0) self.scrollView.addConstraint(trailing) let top = NSLayoutConstraint(item: stackView, attribute: .top, relatedBy: .equal, toItem: self.scrollView, attribute: .top, multiplier: 1.0, constant: 0) self.scrollView.addConstraint(top) let bottom = NSLayoutConstraint(item: stackView, attribute: .bottom, relatedBy: .equal, toItem: self.scrollView, attribute: .bottom, multiplier: 1.0, constant: 0) self.scrollView.addConstraint(bottom) let equalWidth = NSLayoutConstraint(item: stackView, attribute: .width, relatedBy: .equal, toItem: self.scrollView, attribute: .width, multiplier: 1.0, constant: 0) self.scrollView.addConstraint(equalWidth) leading.isActive = true trailing.isActive = true top.isActive = true bottom.isActive = true equalWidth.isActive = true
希望它能帮助你。快乐编码:)在尝试答案时,我偶然发现了如何使用它
class ViewController: UIViewController {
@IBOutlet weak var stack: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func onBtn_Create(_ sender: Any) {
createButton("new button ...")
}
@IBAction func onBtn_Delete(_ sender: Any) {
if let v = stack.arrangedSubviews.last {
stack.removeArrangedSubview(v)
v.removeFromSuperview()
}
}
func createButton(_ title: String) {
let button = UIButton()
button.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
button.backgroundColor = UIColor.blue
button.setTitle(title, for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
stack.addArrangedSubview(button)
}
@objc func buttonAction(sender: UIButton!) {
print("Button tapped")
}
}
我锚定到UIStackView,尾部=0,前导=0,顶部=0,底部=8到TextView.Top
其中的子视图是完整的,没有任何约束
谢谢。我不确定这是否行得通,我想我试过一次,但失败了。到目前为止,你做了什么?你能解决你的问题吗?如果是,请从下面选择一个答案。我们非常感谢您的回答,但没有我需要的确切答案。我一定要买一个吗,tho?谢谢!但我不能理解你的,因为我的技术很差。它是一个循环,用音乐分区和设备信息填充两个堆栈视图。重要的部分是addArrangedSubview,我可以使用addArrangedSubview动态地向StackView添加视图-谢谢,谢谢。好建议。我根本没想过那个滚动视图。我会试试的。欢迎。如果它对你有用,请让我知道,并请接受答案并投票支持我帮助更多。