gpt4 book ai didi

ios - 在 iOS 9 上更改隐藏属性时,UIStackView 不会设置动画

转载 作者:IT王子 更新时间:2023-10-29 05:38:42 25 4
gpt4 key购买 nike

我正在使用 Stack View 创建一种表 UI,我在 StackView 中有 6 个 View ,0、2、4 是可见的,1、3、5 是隐藏的。当点击其中一个可见 View 时,我希望“打开”隐藏的 View 之一。

我的这段代码在 iOS 10 上运行良好,但出于某种原因我无法理解它在 iOS 9 上运行不佳。

请注意,如果我加载所有打开的 View ,关闭动画将起作用,但在将隐藏属性设置为 false 时它不会打开。

这是我的代码-

编辑经过一些调试后,view height constraint 并没有从隐藏中恢复,它的 frame 仍然高度为 0。

 import UIKit

class DeckView: UIView {

}

class ViewController: UIViewController {

var scrollView: UIScrollView!
var stackView: UIStackView!

override func viewDidLoad() {
super.viewDidLoad()

scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)

view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[scrollView]|", options: .alignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))
view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[scrollView]|", options: .alignAllCenterX, metrics: nil, views: ["scrollView": scrollView]))


stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.spacing = 0
stackView.alignment = .center
stackView.distribution = .fillProportionally
stackView.axis = .vertical
scrollView.addSubview(stackView)

scrollView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[stackView]|", options: NSLayoutFormatOptions.alignAllCenterX, metrics: nil, views: ["stackView": stackView]))
scrollView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[stackView]|", options: NSLayoutFormatOptions.alignAllCenterX, metrics: nil, views: ["stackView": stackView]))


for i in 0 ..< 8 {
let view = DeckView()
view.tag = i
view.translatesAutoresizingMaskIntoConstraints = false
view.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true
view.isUserInteractionEnabled = true

if i%2 == 0 {
view.backgroundColor = UIColor.magenta
let constriant = view.heightAnchor.constraint(equalToConstant:160)
constriant.priority = 999
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.openDeck(_:))))
view.addConstraint(constriant)

} else {
view.backgroundColor = UIColor.red
let constriant = view.heightAnchor.constraint(equalToConstant:160)
constriant.priority = 999
view.addConstraint(constriant)
view.isHidden = false
}

stackView.addArrangedSubview(view)
}
}

func openDeck(_ sender:UIGestureRecognizer) {
if let view = sender.view as? DeckView,
let childView = stackView.viewWithTag(view.tag + 1) {
UIView.animate(withDuration: 0.4, animations: {
childView.isHidden = !childView.isHidden
})
}
}
}

最佳答案

  1. 保持 View 的高度优先级低于 1000(为 999)。
  2. 如果它已经隐藏,则不要设置setHidden:true(这是 UIStackView 的错误)

关于ios - 在 iOS 9 上更改隐藏属性时,UIStackView 不会设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40639467/

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