Easiest way to animate a View Controller on top of another? - ios

I have been searching for this solution for a while now, and all the tutorials are hard to follow or they are too specific, so I wanted to ask this question in general terms so it would be easy to implement. I have two separate View Controllers in Xcode (using Obj-C), and I would like to have the second View Controller be animated on top of the first by dragging down anywhere on the screen while having the first view controller stay where it is.
So far, I have tried to cram all this into one View Controller, but this is not practical as there are two different scenes and having two controllers would be a better solution.
Also, if anybody knows how to animate the first controller while the second is sliding over it that would really be a nice addition!

I guess you want to implement Container-Containment view controllers. Here you use parent/child relationship and one view controller can hold many child view controllers. We move child controller to parent position based on criteria.
Hope it helps!

Related

Creating individual View Controllers for each view iOS?

This is a very simple simple question, and I believe the answer is “yes, it’s a best practice,” but I just wanted to ask the question.
When creating new views in your storyboard in Xcode, is it a good idea to create individual custom view controller files to handle each view specifically?
Meaning, if I create a new view called “login quiz,” should I create a “loginQuizViewController” that will handle all the code I write for that view?
Short answer:
Yes, each storyboard scene generally has a unique view controller class associated with it. A view controller has a root view, which in turn may have many subviews below that, and each subview might have further subviews. This collection of views is collectively known as the “view hierarchy”.
Long answer:
Yes, each storyboard “scene” has a view controller associated with it. And generally this view controller class is a particular UIViewController subclass which is unique to that particular Interface Builder scene. But you don’t necessarily have to have a view controller subclass and might, for example, use one of the existing classes (e.g. this is not uncommon for navigation controller scenes or tab bar controller scenes).
But if you have any custom logic associated with a particular storyboard scene, then, yes, you would generally have a unique view controller subclass for that particular scene.
Two minor clarifications:
You refer to the “quiz view”.
That’s fine for colloquial purposes, but for the sake of clarity, when we’re talking about everything for this quiz, it’s really a complex hierarchy of views, not just a one.
A single “quiz” scene will be associated with a unique view controller class, and the instance of that view controller class will have a single “root view” (identified with the view property), but that view will have a whole bunch of subviews (e.g. image views, buttons, labels, etc.), and some of those may have subviews of their own.
So one storyboard scene has its own unique view controller class, but is associated with a whole hierarchy of views.
We often think of a scene, and its associated view controller, as representing everything you might see at any given point in time, but it’s not always a one-to-one relationship. Sometimes what you see is composed of several storyboard scenes and their respective view controllers.
For example, if you are using a navigation controller, the navigation controller takes care of the navigation bar at the top, and your view controller might take care of everything under the navigation bar. What is visible on screen is composed of these two view controllers.
Or, in a more extreme example, we can define our own view controller containers. In the example below, when we present A, we can define the bottom half of the screen to be managed by a completely separate scene, B, which has its own view controller:
In this case, both A and B have their own IB scenes and their own respective view controllers. Achieve this by adding a “container view” to the A scene (the light blue UIView in the above screen snapshot).
But the main point is that what you see on screen may be captured by a single storyboard scene and view controller, or it might be composed of several.
A view controller can and probably will contain multiple views apart from the root view it has. That means that usually a view controller owns one ore more views that are subview's of it's own root view. Those views are often also controlled by the same view controller.
When coming from the storyboard most items you see are actually representations of view controllers (others are placeholders). "Login Quiz" sounds like a screen in conceptual terms, so (without knowing your details) it would probably make sense to create a LoginQuizViewController.

Is it better to have a viewDidLoad in all view controllers or just in the main view controller have multiple UIViews?

For now I have a single login view controller that hide and adds subviews dynamically based on user interaction. Is it better to have multiple UIViews with one view controller, or multiple view controllers?
The benefit of having children UIViewControllers would be if you needed to care about the view lifecycle in any of the children. For instance, if in some of your subviews (child views) you needed to know if it appeared to trigger some logic, then it would be good to use children UIViewControllers.
Another reason might be, if you wanted to use view controller transitions within your custom container, as described by Apple in the UIViewController class reference:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instm/UIViewController/transitionFromViewController:toViewController:duration:options:animations:completion:
But, to me it doesn't sound like you would need this, it sounds like you would just show/hide your views, or otherwise animate them.
But if your views don't care about any of that, and just need to render UI elements, I'd say using children UIViewControllers adds additional complexity without much benefit.
This is under the assumption that either way, all your views are living on the single UIViewController's view. If you are pushing and popping view controllers inside a UINavigationController, or presenting ViewControllers modally, you definitely should use UIViewControllers for that.
Also, you definitely shouldn't put all your view code inside that single view controller. You should use subclasses of UIView, or some other mechanism of making sure you separate your components.
For example you might have:
LoginViewController - organizes and hides and shows the individual views
SignInView - shows the sign in form
RegisterView - shows the register form
...etc
Of course its better to have multiple view controllers for many reasons:
Each view controller has its own code/logic/functionality.
Better for memory management. Once you're done from a view controller, system will automatically deallocates it from memory. Using multiple views will be overload in your system when some views are not used.
Better organisation for your project. You will just present/dismiss/push/pop view controllers and pass data between each other. Instead you will deal with horrible logic for hiding and showing UIViews.
Using one view controller will have a massive amount of code which in long term the project will be an impossible mission to manage.
Having multiple view controllers is better in the sense that you would have more ease at coding them individually. Also Navigation will be good which is good for User Experience.
Memory Management will be efficient.
The MVC architecture(Model View Controller) will instead become Massive View Controller which will be a headache while debugging.
Check out this answer for more clarity. I think you are confused between ViewController and View.
Check out the accepted answer of this question in the link. Hope it helps.
About viewController's "viewDidLoad" and "viewWillAppear" methods

View Container vs Multiple ViewControllers vs Hidden Views Single ViewController

I have a View Controller and need to segue into another View Controller (which is customized based on what scenario I want to show). Trying to decide what is the best approach here in regards to simplicity vs efficiency
Three options I can think of:
(a) Have my View Controller segue into a View Controller that holds a View Container linked to multiple View Controllers
(b) Have my View Controller segue into a View Controller with multiple views that can be hidden and rearranged
(c) Have my View Controller segue into different View Controllers depending upon the criteria
I'm still uncertain how much each View Controller should differ from each other, but given that one View Controller might segue into another View Controller that's irrelevant for the other two scenarios, what are your guys' thoughts on the three approaches in terms of code complexity, ease of use, general efficiency in terms of speed/memory management. It's possible that the View Controllers that are being segued into might differ by just a little, but also by a lot!
I think all depends on difference between viewControllers for each scenario. BUT what I can certainly say is do not use varriant (B), because even if difference seems tiny now, as only it begins growing, your code that compose views will becomes more complicated and unreadable, at least in my case it usually happens. So, if you must show different viewControllers with same or pretty similar use (e.g. save for, show list, select item from list. go next, go previouse), try to use one viewController and load different views that are already composed. And if usage of those controllers different - use multiple controllers.

Building a slide out menu with container view controllers and the Storyboard. I'm very confused

I'm interested in building a slide out sidebar for a view controller, and I've done a lot of reading on how to implement it. This tutorial, as well as virtually all the others go over creating child view controllers, which seems like a great way to go about it. However, it's always done in code for examples.
This post goes over how to create container view controllers in the Storyboard, but it left me confused how exactly container views work in the Storyboard, and there seems to be little in the way of explanations online.
Finally, this post went over how to implement a slide out menu with container view controllers and a Storyboard. Sounds perfect right? Well, in reading it I was confused how the author seems to use multiple container views. I thought there was supposed to be one that encompassed all of the subviewcontrollers. Is it implemented correctly in that post?
Basically, I'd love an explanation on how to implement multiple view controllers using container view controllers via the Storyboard. It's my understanding from the first tutorial that you have the view controller that contains everything, and then add view controllers to that container and then they all work together via delegates. Achieving this in Storyboards is leaving me scratching my head.
Adding a container basically adds a child view controller and its view to the main controller. I think you are not clear on two things here. First, each container can be linked to only 1 controller/view pair. So, if you want to have more than one child controllers, you must have more than one containers. Secondly, communication between parent and child controllers is handled by 'embed segues', not by delegates.

Problems using split view controllers in iOS

I'm new at objective-c programming and I need help clearing some doubts about the split view controllers. I understand that if you use a split view controller in your app then it must necessarily be your root view and I was wondering if there was any way to get around it.
I mean, I need to use split view controllers in my app but only in a couple of views, and for the rest just use simple views. If anyone has any idea or suggestion as to how to do this I'd be really grateful.
There are several things you can do. You can present your simple views modally, which will cover up the split view controller, or you can switch out the window's root view controller when you're done with the split view. Which to do depends on how and where in your app you use the split view. If you are going back and forth to it, then I would use the first method, if you're using it once, then going to the simple views, I would use the second method.
You can add split view controllers as children of tabbarviewcontrollers. Also, you can add split view controllers into container view controllers you build.

Resources