[英]UIStackView won't animate when changing the hidden property on iOS 9
I am using a Stack view to create a kind of table UI, I have 6 views in a StackView 0,2,4 are visible and 1,3,5 are hidden. 我使用Stack视图来创建一种表UI,我在StackView中有6个视图0,2,4是可见的,1,3,5是隐藏的。 When tapping one of the visible views I wish to "open" one of the views that are hidden.
当点击其中一个可见视图时,我希望“打开”其中一个隐藏的视图。
I have this code that works great on iOS 10 but from some reason I can not understand it is not working well on iOS 9. 我有这个代码在iOS 10上运行良好,但由于某些原因我无法理解它在iOS 9上运行不佳。
Note that if I load the views all open, the close animation will work but it won't open when setting the hidden property to false. 请注意,如果我将视图全部打开,则关闭动画将起作用,但在将hidden属性设置为false时不会打开。
Here is my code - 这是我的代码 -
EDIT After some debugging looks like the view height constraint is nor recovering from the hiding, and it's frame is still height is 0. 编辑经过一些调试看起来视图高度约束也没有从隐藏中恢复,它的帧仍然是高度为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
})
}
}
}
setHidden:true
if it is already hidden(This is UIStackView's bug) setHidden:true
如果它已被隐藏,则setHidden:true
(这是UIStackView的错误) If any one stumble on this issue. 如果有人在这个问题上绊倒了。
I was able to solve this issue by removing the - 通过删除 - 我能够解决这个问题 -
stackView.distribution = .fillProportionally
I am not sure why this happened but I found that Autolayout added a height constraint named 'UISV-fill-proportionally' with a constant of 0 and greater priority then my height constraint. 我不知道为什么会发生这种情况,但我发现Autolayout添加了一个名为'UISV-fill-proportionally'的高度约束,其常量为0,优先级高于我的高度约束。 removing the fillProportionally fixed the issue.
删除fillProportionly修复了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.