Android迅速将iOS的模拟片段化 [英] Android fragments analog in iOS's swift
问题描述
我正在Swift中迈出第一步(已完成3天),并且需要Android片段之类的东西
I'm making my first steps in Swift (3 days into it) and need something like Android's fragments
我需要做什么:
使用自己的控制器在xib中有一些视图,并将它们充气到某个视图容器中,以便它们可以填充所有容器.
have some views in xibs with it's own controllers and inflate them to some view container so they could fill all the container.
例如,我有一个菜单会出现在iPad上屏幕的左侧,而在iPhone上,该菜单将隐藏在抽屉中.通过点击菜单项,我将需要的Fragment填充到一个容器中,在iPad的情况下,它占据屏幕右侧的3/4,在iPhone的情况下,它占据整个屏幕.
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.
通过这种方法,我不必担心Fragment视图显示在何处,它占据屏幕的哪一部分(或者可能是整个屏幕),因为它是一个单独的Fragment,它具有自己的控制器,可以被另一个片段替换(或堆叠),如果我想返回堆栈,则上一个Fragment将成为焦点
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
iOS是否有类似的产品,或者该如何实现?
Is there such an analog for iOS or how can I implement this?
推荐答案
要实现类似于片段的内容,必须在View Controller中使用Container View,并在其中填充childViewControllers.
For implementing something analogous to fragments, you must use Container View in your View Controller with childViewControllers being populated inside it.
文档:容器视图控制器
您可以在Xcode的Interface Builder的对象库中找到"Container View"元素.
You can find the 'Container View' element in the object Library of your Interface Builder in Xcode.
此外,您可以创建新的视图控制器,并通过以下代码添加该视图控制器:
Also, you can create the new view controller that you want to add it through code as follows:
Obj-C代码:
//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];
}
这篇关于Android迅速将iOS的模拟片段化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!