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

时间:2016-11-16 18:21:18

标签: ios swift animation uistackview

我使用Stack视图创建一种表UI,我在StackView中有6个视图0,2,4是可见的,1,3,5是隐藏的。当点击其中一个可见的视图时,我希望"打开"其中一个隐藏的观点。

我有这个代码在iOS 10上运行良好,但由于某些原因我无法理解它在iOS 9上运行不佳。

请注意,如果我将视图全部打开,则关闭动画将起作用,但在将hidden属性设置为false时它不会打开。

这是我的代码 -

修改 经过一些调试后,视图高度约束似乎也没有从隐藏中恢复,它的帧仍然是高度为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
            })
    }
}
}

2 个答案:

答案 0 :(得分:36)

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

答案 1 :(得分:3)

如果有人在这个问题上绊倒。

我能够通过删除 -

来解决这个问题
stackView.distribution = .fillProportionally

我不确定为什么会发生这种情况,但我发现Autolayout添加了一个名为“UISV-fill-proportionalally&#39;使用0和更高优先级的常量然后我的高度约束。删除fillProportionly修复了问题。