gpt4 book ai didi

ios - 索引处的 Swift 方法 insertSubview 效果很好。但是如何重新定位所有 subview 以便为插入的 subview 腾出空间

转载 作者:行者123 更新时间:2023-11-30 12:21:59 25 4
gpt4 key购买 nike

我以编程方式向特定 View 添加了 4 个 subview (label1、label2、label3、label4)和约束(label2 顶部取决于 label1 底部等)。现在我使用 insertSubiewAtIndex 方法在索引 1(label5) 处插入另一个 subview 。现在如何刷新 UI,因为约束是动态的,意味着 label5 应该占据 label1 位置,label1 应该占据 label2 位置,依此类推。

这是我的代码

override func viewDidLoad() {
super.viewDidLoad()

let label1 = UILabel()
label1.translatesAutoresizingMaskIntoConstraints = false
label1.text = "label1"
label1.numberOfLines = 0
label1.tag = 1

self.containerView.addSubview(label1)

NSLayoutConstraint(item: label1, attribute: .top, relatedBy: .equal, toItem: self.containerView, attribute: .top, multiplier: 1.0, constant: 16.0).isActive = true

NSLayoutConstraint(item: label1, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1.0, constant: 0.0).isActive = true

NSLayoutConstraint(item: label1, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1.0, constant: 0.0).isActive = true


let label2 = UILabel()
label2.translatesAutoresizingMaskIntoConstraints = false
label2.text = "label2"
label2.numberOfLines = 0
label2.tag = 1

self.containerView.addSubview(label2)

NSLayoutConstraint(item: label2, attribute: .top, relatedBy: .equal, toItem: self.containerView.subviews[self.containerView.subviews.count - 2], attribute: .top, multiplier: 1.0, constant: 16.0).isActive = true

NSLayoutConstraint(item: label2, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1.0, constant: 0.0).isActive = true

NSLayoutConstraint(item: label2, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1.0, constant: 0.0).isActive = true


let label3 = UILabel()
label3.translatesAutoresizingMaskIntoConstraints = false
label3.text = "label3"
label3.numberOfLines = 0
label3.tag = 1

self.containerView.addSubview(label3)

NSLayoutConstraint(item: label3, attribute: .top, relatedBy: .equal, toItem: self.containerView.subviews[self.containerView.subviews.count - 2], attribute: .top, multiplier: 1.0, constant: 16.0).isActive = true

NSLayoutConstraint(item: label3, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1.0, constant: 0.0).isActive = true

NSLayoutConstraint(item: label3, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1.0, constant: 0.0).isActive = true


let label4 = UILabel()
label4.translatesAutoresizingMaskIntoConstraints = false
label4.text = "label4"
label4.numberOfLines = 0
label4.tag = 1

self.containerView.addSubview(label4)

NSLayoutConstraint(item: label4, attribute: .top, relatedBy: .equal, toItem: self.containerView.subviews[self.containerView.subviews.count - 2], attribute: .top, multiplier: 1.0, constant: 16.0).isActive = true

NSLayoutConstraint(item: label4, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1.0, constant: 0.0).isActive = true

NSLayoutConstraint(item: label4, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1.0, constant: 0.0).isActive = true

let label5 = UILabel()
label5.translatesAutoresizingMaskIntoConstraints = false
label5.text = "label5"
label5.numberOfLines = 0
label5.tag = 1

self.containerView.addSubview(label5)

NSLayoutConstraint(item: label5, attribute: .top, relatedBy: .equal, toItem: self.containerView.subviews[0], attribute: .top, multiplier: 1.0, constant: 16.0).isActive = true

NSLayoutConstraint(item: label5, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1.0, constant: 0.0).isActive = true

NSLayoutConstraint(item: label5, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1.0, constant: 0.0).isActive = true

}

最佳答案

override func viewDidLoad() {
super.viewDidLoad()

containerView.backgroundColor = .white
let label1 = UILabel()
label1.translatesAutoresizingMaskIntoConstraints = false
label1.text = "label1"
label1.numberOfLines = 0
label1.tag = 1

self.containerView.addSubview(label1)

let label2 = UILabel()
label2.translatesAutoresizingMaskIntoConstraints = false
label2.text = "label2"
label2.numberOfLines = 0
label2.tag = 1

self.containerView.addSubview(label2)

let label3 = UILabel()
label3.translatesAutoresizingMaskIntoConstraints = false
label3.text = "label3"
label3.numberOfLines = 0
label3.tag = 1

self.containerView.addSubview(label3)

let label4 = UILabel()
label4.translatesAutoresizingMaskIntoConstraints = false
label4.text = "label4"
label4.numberOfLines = 0
label4.tag = 1

self.containerView.addSubview(label4)

let label5 = UILabel()
label5.translatesAutoresizingMaskIntoConstraints = false
label5.text = "label5"
label5.numberOfLines = 0
label5.tag = 1

self.containerView.addSubview(label5)

setConstraint(to: [label5, label1, label2, label3, label4])
}

private func setConstraint(to views: [UIView]) {
var iterator = views.makeIterator()
var prev: UIView = self.containerView
while let next = iterator.next() {
NSLayoutConstraint(item: next, attribute: .top, relatedBy: .equal, toItem: prev, attribute: .top, multiplier: 1, constant: 16).isActive = true
NSLayoutConstraint(item: next, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1, constant: 0).isActive = true

NSLayoutConstraint(item: next, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1, constant: 0).isActive = true
prev = next
}
}

关于ios - 索引处的 Swift 方法 insertSubview 效果很好。但是如何重新定位所有 subview 以便为插入的 subview 腾出空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44690550/

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