gpt4 book ai didi

ios - 以编程方式重新排序 subview 在 tableViewCell 中不起作用

转载 作者:行者123 更新时间:2023-11-30 13:38:01 25 4
gpt4 key购买 nike

我正在尝试重新排序图像和堆栈 View (此处称为 labelsStack),它们都包含在另一个堆栈 View (此处称为堆栈)中。我的目标是以编程方式反转两个 subview 的索引顺序,以便在运行时更改它们的位置(它们是水平分布的,所以理论上,如果我重新排序它们的索引,它应该在自动布局中重新排序它们)

我尝试从Apple文档更新索引、交换 subview 、sendViewForward等,但它不起作用,这是我的tableViewCell的代码:

     override func awakeFromNib() {
super.awakeFromNib()
// Initialization code


}

override func layoutSubviews() {
cellImage.layer.cornerRadius = cellImage.bounds.height / 3
cellImage.clipsToBounds = true

if incoming {
} else {
// as one of the many methods that didn't work
self.stack.insertSubview(cellImage, belowSubview: labelsStack)
}


}

最佳答案

您可以创建两组替代约束,一组用于默认顺序,另一组用于相反顺序。默认约束是以 1 结尾的约束

enter image description here

要最初停用替代设置,您可以使用检查器并更改已安装复选框

enter image description here

你的 View Controller 现在应该有 6 个导出:

@IBOutlet weak var topViewTopConstraint1: NSLayoutConstraint!
@IBOutlet weak var bottomViewTopConstraint1: NSLayoutConstraint!
@IBOutlet weak var bottomViewBottomConstraint1: NSLayoutConstraint!

@IBOutlet weak var topViewTopConstraint2: NSLayoutConstraint!
@IBOutlet weak var bottomViewTopConstraint2: NSLayoutConstraint!
@IBOutlet weak var topViewBottomConstraint2: NSLayoutConstraint!

现在激活您可以使用的另一组约束:

    self.topViewBottomConstraint2.active = true
self.topViewTopConstraint2.active = true
self.bottomViewTopConstraint2.active = true
self.topViewTopConstraint1.active = false
self.bottomViewBottomConstraint1.active = false
self.bottomViewTopConstraint1.active = false

反之亦然

关于ios - 以编程方式重新排序 subview 在 tableViewCell 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35876352/

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