Equivalent of Fragment (Android) in iOS
Asked Answered
Z

1

33

I want to know if there is a way to use some view controllers within a view controller which provides the same functionality as Fragments in Android? I want to use custom view controllers so I do not want to use NSTabViewController, UISplitViewController etc.

Zebulen answered 27/1, 2015 at 10:0 Comment(0)
O
46

Container View Controllers allow to include child view controller inside another view controller. Take a look at the docs by clicking on the "more..." link in the class overview.

Implementing a Container View Controller
A custom UIViewController subclass can also act as a container view controller. A container view controller manages the presentation of content of other view controllers it owns, also known as its child view controllers. A child's view can be presented as-is or in conjunction with views owned by the container view controller....

Using them in storyboards is as simple as dragging the Container View to your view controller.

enter image description here

And Xcode will automatically add a child view controller to it

enter image description here

Oster answered 27/1, 2015 at 10:6 Comment(2)
Container View is not available when using Xib's though. My approach is to add a view controller in code by calling - (void)addChildViewController:(UIViewController *)childController; -> developer.apple.com/documentation/uikit/uiviewcontroller/…Ceremony
This answer gives you also a hint: stackoverflow.com/a/32554359 and the Apple documentation can help you: developer.apple.com/library/archive/featuredarticles/…Ceremony

© 2022 - 2024 — McMap. All rights reserved.