Apologies
You must be signed in to watch this lesson.
Home / Menu View & Constraint Animations
Twitter Slide Out Menu
A much more efficient way of performing animations is to utilize the power Auto Layout Constraints. In today's lesson, we'll set up a very simple scenario of animating two blocks in our application. This example will illustrate the technique that we can use to animate our home and menu screens together. The great thing about this strategy is that it preserves the correct layout even after orientation changes.

Comments (13)
Michael Evans
5 years ago
Hi Brian I added homeView to redView and set up all the constraints. homeView shows up. When I add the homeView.translatesAutoresizingMaskIntoConstraints = false the homeView disappears. The setupViewControllers function is being called and if I comment out the homeView.translatesAutoresizingMaskIntoConstraints = false line, the homeView shows up again. Any thoughts?
Brian Voong
5 years ago
Michael Evans
5 years ago
Nevermind. I had set the homeView.leadingAnchor to redView.trailingAnchor. Works now
Michael Evans
5 years ago
Thanks for the quick response. It had me pulling out my hair for an hour!
Brian Voong
5 years ago
iamparne
5 years ago
Hey Brian, could you please leave the link to the reference for `setNeedsLayout`, that you were talking about in the video?
Brian Voong
5 years ago
iamparne
5 years ago
Thanks Brian, for the quick reply :)
Jeffrey Chang
5 years ago
In case you still need it https://stackoverflow.com/questions/31361842/why-layoutifneeded-allows-to-perform-animation-when-updating-constraints https://www.twilio.com/blog/2018/04/constraint-animations-ios-apps-xcode-swift.html
iamparne
5 years ago
Thanks man, really appreciate it :)
iamparne
5 years ago
These are the same links in the description though.
Jeffrey Chang
5 years ago
that is the link he's talking about. Is there anything that you don't understand about layoutIfNeed()? such why layout wouldn't update without it.
iamparne
5 years ago
I think I understand it now, correct me if I am wrong. My understanding is that the UIView's animate function does not actually update the layout when the constraints are changing but instead they just update it everything at once after the animation is done but we want to continuously update the constraints during the animation and that is why we are calling `layoutIfNeeded()` right?
Jeffrey Chang
5 years ago
Correct. This happen during a run loop cycle. it will make a synchronous call that tells the system you want a layout and redraw of a view and its subviews, and you want it done immediately without waiting for the update cycle. Below i have definition of what is run loop cycle. https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-SW1
iamparne
5 years ago
Awesome, Thanks for the reference Jeffrey!
Tube
5 years ago
I learned it as Top, Right, Bottom, Left, following clockwise from the origin.
Paweł Liczmański
5 years ago
when I rotate simulator to horizontal when menu is opened and im trying to swipe to right, menu is closing why? In vertical we dont have this problem. We cant swipe to right when menu is opened because of min and max of translate.x
langrulez
5 years ago
Hey Brain, I use the same code as you and everything works great, but if I use a static table in the HomeController, the content is not displayed. Do you have any idea why?
Alfrad
5 years ago
Hi Brian, after completing this section "Auto Layout Animations", I have 2 queries which puzzled me: 1) Why do you prefer to set up auto layout constraints on a new swift file (Base Sliding Controller) instead of working directly on Home and Menu Controller? This resulted in some duplicated codes in both Base Sliding Controller and Home Controller such as setting up dark mode, handling pan gesture, setting the sliding velocity, etc. 2) In Home Controller, we need to set up CG Affine Transform for the pan gesture whereas in the Base Sliding Controller this is not required? Thank you.
Brian Voong
5 years ago
LucienChu
4 years ago
Hello Brian It is so fun that there is still some UI/UX bug for a well written app, it is also frustrated that we have to fixed them one by one if we are lucky, if it was not, we have to changed the whole structure, just like from last section to this section. Thank you to teach me that I can extract a view from a view controller. Too obsessive with IB before, never know I could do that. One question, would you mind telling me, or giving me a hint, why you have to disable the gesture in MenuViewController in the section in order to make the gesture set in the BaseSlidingViewController work? In deed, I think we only need the animation and gesture in the BaseSlidingViewController controller works properly is good enough since it controls two subView in single viewConller(BaseSlidingViewController) Thanks and have a good weekend.
Brian Voong
4 years ago
aldo3
4 years ago
Hi Brian, The base view controller is a Container View correct? If so, is the method didMove(toParent:) optional? Thanks
malrhex
4 years ago
Hi Brian, how can I address this issue? fileprivate func setupViewControllersForMenuSetUp() { // let's add back our HomeController into the redView //MainTabBar is gone, Comments are gone. let homeController = HomeController(collectionViewLayout: UICollectionViewFlowLayout()) Problem: When I set up the redView to be the homeController my TabBar is gone as well tapping on my comments icon isn't working.
malrhex
4 years ago
now I have two instances of HomeVC. the first one is with the tabBbar and the second doesn't have it. RootVC: MainTabBar. I've been trying to figuring out how can I add the "blueView" to the first instance of HomeVC. But only if I can take the view of a VC can add it to the constraints... but idk this is pretty advanced.
malrhex
4 years ago
And... why my dummy cells saying "Menu item row: #" isn't displaying If already added the addChild(menuController) it is just displaying the rows without any content.
malrhex
4 years ago
everything from 8:15 to 10:40
malrhex
4 years ago
I got news, my view hierarchy is saying that the tableView is just a UIView, instead of your project saying that the same view it is nameofproject.menuController > tableView... why could be that?, I have my addChild(menuController) any ideas?
MehmetEmre
4 years ago
Hey everyone. A quick question. when i try to "//window?.rootViewController = BaseSlidingController()" in app delegate . Program gives a crash like "UICollectionView must be initialized with a non-nil layout parameter' " like this. How can I solve this problem can you help me ? Note I am suing storyboard. thank you
Brian Voong
4 years ago
MehmetEmre
4 years ago
Thank you
MehmetEmre
4 years ago
Sorry when I put this code at my collectionviewcontroller (its name HomeController) ( above viewdidload()) and gives Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee2ed1fc0) ( in my data struct array ).. I made some search on net but I didn't find anything. can you help me ?
harry528tt
4 years ago
why do you have the almost same logic in baseVC??? You already implemented most of them in the HomeVC
HELP & SUPPORT