Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android fragments analog in iOS's swift

I'm making my first steps in Swift (3 days into it) and need something like Android's fragments

What I need to do :

have some views in xibs with it's own controllers and inflate them to some view container so they could fill all the container.

For example, I have a menu that will be in a left side of the screen on an iPad, and on iPhone this menu will be hidden in a drawer. By tapping on a menu item I inflate needed Fragment into a container that in the case of iPad takes 3/4 of the right side of the screen and in the case of iPhone it takes full screen.

With such approach I don't have to worry about where my Fragment view is showing, what part of a screen (or may be, the entire screen) it is occupying as it is a separate Fragment with it's own controller that can be substituted (or stacked) with another fragments and if I want to go backstack , previous Fragment will come in focus

Is there such an analog for iOS or how can I implement this?

like image 883
Vlad Alexeev Avatar asked Dec 04 '15 08:12

Vlad Alexeev


1 Answers

For implementing something analogous to fragments, you must use Container View in your View Controller with childViewControllers being populated inside it.

Documentation: Container View Controllers

You can find the 'Container View' element in the object Library of your Interface Builder in Xcode.

Container view

Also, you can create the new view controller that you want to add it through code as follows:

Obj-C code:

//To add child VC
- (void) displayContentController: (UIViewController*) content;
{
    [self addChildViewController:content];                 
    content.view.frame = self.containerView.bounds;
    [self.containerView addSubview:content.view];
    [content didMoveToParentViewController:self];          
}

//To remove child VC
- (void) hideContentController: (UIViewController*) content
{
    [content willMoveToParentViewController:nil];  
    [content.view removeFromSuperview];            
    [content removeFromParentViewController];      
}
like image 183
Vijay Tholpadi Avatar answered Sep 29 '22 06:09

Vijay Tholpadi