I’m trying to animate a view to hide behind the navigation bar.
The idea is the yellow label to appear from behind the green view.
I tried this modifying the top constraint to a negative number, but it works but if the yellow view is bigger than the green one it ends over the safe area.
My code:
@IBAction func buttonClick(_ sender: Any) {
UIView.animate(withDuration: 0.5) {
if(self.topMargin.constant<0){
self.topMargin.constant=0
}else {
self.topMargin.constant = -100
}
self.view.layoutIfNeeded()
}
}
Thats the result when hidden:
How can I achieve this effect without invading the safe zone?
2
Answers
setup your label under your Controller class:
After that set two variables for label animation state:
In viewDidLoad setup your nav bar (I set it with my extension), present your label and constraints:
Now add a variable to control state of label and animation func:
The result:
Embed your label (or whatever view you want to animate) in a "holder" view, constrained to the safe-area, with
.clipsToBounds = true
…The holder view background will normally be clear — I’m toggling it between clear and red so you can see it’s frame.
Here’a quick example code for that: