1. First, add a container view to your view controller. I applied a blue color to the child view controller so you could tell the difference between the two.
2. Next, position and constrain the container view however you want it to appear in your parent view controller.
3. Then drag a second container view right on top of container view A as shown here.
4. Constrain container view B to have equal width and height to container view A, and center container view B to the middle of container view A. This way, any layout changes that happen to our first container view will be applied to the second. They should both be the same size and have the same position.
class ViewController: UIViewController {
@IBOutlet weak var containerViewA: UIView!
@IBOutlet weak var containerViewB: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func showComponent(_ sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0 {
UIView.animate(withDuration: 0.5, animations: {
self.containerViewA.alpha = 1
self.containerViewB.alpha = 0
})
} else {
UIView.animate(withDuration: 0.5, animations: {
self.containerViewA.alpha = 0
self.containerViewB.alpha = 1
})
}
}
}
沒有留言:
張貼留言