以下是重现此问题的代码。您可以将其粘贴到Playground来执行
//: Playground - noun: a place where people can play
import UIKit
import AVKit
import PlaygroundSupport
let root = UIView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 200, height: 200)))
root.backgroundColor = UIColor.white
let container = UIStackView()
container.translatesAutoresizingMaskIntoConstraints = false
container.axis = .vertical
container.backgroundColor = UIColor.red
container.heightAnchor.constraint(equalToConstant: 140).isActive = true
root.addSubview(container)
container.distribution = .fill
let colors = [UIColor.yellow, .green]
for i in 0..<4 {
let child = UILabel(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 100, height: 50)))
child.translatesAutoresizingMaskIntoConstraints = false
child.backgroundColor = colors[i%colors.count]
if i == 0 || i == 3 {
child.heightAnchor.constraint(equalToConstant: 20).isActive = true
} else {
child.heightAnchor.constraint(equalToConstant: 50).isActive = true
}
child.widthAnchor.constraint(equalToConstant: 200).isActive = true
child.text = String(i)
container.addArrangedSubview(child)
}
PlaygroundPage.current.liveView = root
PlaygroundPage.current.needsIndefiniteExecution = true
DispatchQueue.global().async {
Thread.sleep(forTimeInterval: 2)
DispatchQueue.main.async {
UIView.transition(with: container, duration: 3, options: [.showHideTransitionViews], animations: {
container.subviews[2].isHidden = true
}, completion: nil)
}
}
将要隐藏的视图将始终超出父边界。我尝试将clipToBounds
设置为父级,但是它不起作用。