执行过渡动画时,视图将移出父边界

时间:2018-12-29 08:04:43

标签: ios

以下是重现此问题的代码。您可以将其粘贴到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)
    }
}

问题

enter image description here

将要隐藏的视图将始终超出父边界。我尝试将clipToBounds设置为父级,但是它不起作用。

1 个答案:

答案 0 :(得分:0)

似乎UIStackView不会裁剪子视图。因此,将UIView包裹起来并设置clipToBounds=true将解决此问题。

link