简体   繁体   English

如何将 UIViewController 的视图添加为子视图

[英]How to add an UIViewController's view as subview

Note to googlers, this QA is now six years out of date !谷歌员工请注意,这个 QA 现在已经过时了 6 年

As Micky below and others mention, this is now done on an everyday basis with Containers in iOS.正如下面的 Micky 和其他人提到的,这现在每天都在 iOS 中使用容器来完成。


I have a ViewController which controls many subviews.我有一个 ViewController 控制许多子视图。 When I click one of the buttons I initialize another viewcontroller and show it's view as the subview of this view.当我单击其中一个按钮时,我会初始化另一个视图控制器并将其视图显示为该视图的子视图。 However the subview exceeds the bounds of the frame for subview and infact fills the entire screen.然而,子视图超出了子视图框架的边界,实际上填满了整个屏幕。

What could be wrong?可能有什么问题? I presume the problem is that UIViewController's view has a frame (0,0,320,460) and hence fills the entire screen (though it receive's touch events only when touched within the subview frame bounds).我认为问题在于 UIViewController 的视图有一个框架 (0,0,320,460),因此填满了整个屏幕(尽管它仅在子视图框架边界内触摸时才会接收触摸事件)。 How can I resize the frame to fit as subview.如何调整框架大小以适合子视图。

In short, I need help adding a viewcontroller's view as a subview to another viewcontroller's view.简而言之,我需要帮助将一个视图控制器的视图作为子视图添加到另一个视图控制器的视图中。

Thanks!谢谢!

As of iOS 5 , Apple now allows you to make custom containers for the purpose of adding a UIViewController to another UIViewController particularly via methods such as addChildViewController so it is indeed possible to nest UIViewControllers 从 iOS 5 开始,Apple 现在允许您制作自定义容器,以便将UIViewController添加到另一个UIViewController 中,特别是通过addChildViewController等方法,因此确实可以嵌套UIViewController

EDIT: Including in-place summary so as to avoid link breakage编辑:包括就地摘要以避免链接中断

I quote:我引用:

iOS provides many standard containers to help you organize your apps. iOS 提供了许多标准容器来帮助您组织应用程序。 However, sometimes you need to create a custom workflow that doesn't match that provided by any of the system containers.但是,有时您需要创建与任何系统容器提供的工作流都不匹配的自定义工作流。 Perhaps in your vision, your app needs a specific organization of child view controllers with specialized navigation gestures or animation transitions between them.也许在您的愿景中,您的应用程序需要一个特定的子视图控制器组织,在它们之间具有专门的导航手势或动画转换。 To do that, you implement a custom container - Tell me more...为此,您需要实现一个自定义容器- 告诉我更多...

...and: ...和:

When you design a container, you create explicit parent-child relationships between your container, the parent, and other view controllers, its children - Tell me more当你设计一个容器时,你会在你的容器、父视图和其他视图控制器及其子视图之间创建显式的父子关系- 告诉我更多

Sample (courtesy of Apple docs) Adding another view controller's view to the container's view hierarchy示例(由 Apple 文档提供)将另一个视图控制器的视图添加到容器的视图层次结构

- (void) displayContentController: (UIViewController*) content
{
   [self addChildViewController:content];                 
   content.view.frame = [self frameForContentController]; 
   [self.view addSubview:self.currentClientView];
   [content didMoveToParentViewController:self];          
}

Thanks to this guys I did it http://highoncoding.com/Articles/848_Creating_iPad_Dashboard_Using_UIViewController_Containment.aspx感谢这些家伙,我做到了http://highoncoding.com/Articles/848_Creating_iPad_Dashboard_Using_UIViewController_Containment.aspx

Add UIView, connect it to header:添加 UIView,将其连接到标题:

@property (weak, nonatomic) IBOutlet UIView *addViewToAddPlot;

In - (void)viewDidLoad do this:在 - (void)viewDidLoad 中这样做:

ViewControllerToAdd *nonSystemsController = [[ViewControllerToAdd alloc] initWithNibName:@"ViewControllerToAdd" bundle:nil];
    nonSystemsController.view.frame = self.addViewToAddPlot.bounds;
    [self.addViewToAddPlot addSubview:nonSystemsController.view];
    [self addChildViewController:nonSystemsController];
    [nonSystemsController didMoveToParentViewController:self];

Enjoy享受

This answer is correct for old versions of iOS, but is now obsolete.这个答案对于旧版本的 iOS 是正确的,但现在已经过时了。 You should use Micky Duncan's answer, which covers custom containers.您应该使用 Micky Duncan 的答案,其中涵盖了自定义容器。

Don't do this!不要这样做! The intent of the UIViewController is to drive the entire screen. UIViewController的意图是驱动整个屏幕。 It just isn't appropriate for this, and it doesn't really add anything you need.它只是不适合这个,它并没有真正添加任何你需要的东西。

All you need is an object that owns your custom view.您只需要一个拥有自定义视图的对象。 Just use a subclass of UIView itself, so it can be added to your window hierarchy and the memory management is fully automatic.只需使用UIView本身的子类,就可以将其添加到您的窗口层次结构中,并且内存管理是全自动的。

Point the subview NIB's owner a custom subclass of UIView .将子视图 NIB 的所有者指向UIView的自定义子类。 Add a contentView outlet to this custom subclass, and point it at the view within the nib.向这个自定义子类添加一个contentView插座,并将其指向笔尖内的视图。 In the custom subclass do something like this:在自定义子类中做这样的事情:

- (id)initWithFrame: (CGRect)inFrame;
{
    if ( (self = [super initWithFrame: inFrame]) ) {
        [[NSBundle mainBundle] loadNibNamed: @"NibNameHere"
                                      owner: self
                                    options: nil];
        contentView.size = inFrame.size;
        // do extra loading here
        [self addSubview: contentView];
    }
    return self;
}

- (void)dealloc;
{
    self.contentView = nil;
    // additional release here
    [super dealloc];
}

(I'm assuming here you're using initWithFrame: to construct the subview.) (我在这里假设您使用initWithFrame:来构建子视图。)

I feel like all of these answers are slightly incomplete, so here's the proper way to add a viewController's view as a subview of another viewController's view:我觉得所有这些答案都有些不完整,所以这是将 viewController 的视图添加为另一个 viewController 视图的子视图的正确方法:

    [self addChildViewController:viewControllerToAdd];
    [self.view addSubview:viewControllerToAdd.view];
    [viewControllerToAdd didMoveToParentViewController:self];

If you'd like, you can copy this into your code as a snippet.如果您愿意,可以将其作为片段复制到您的代码中。 SO doesn't seem to understand code replacement formatting, but they will show up clean in Xcode: SO 似乎不理解代码替换格式,但它们会在 Xcode 中清晰显示:

    [self addChildViewController:<#viewControllerToAdd#>];
    [self.view addSubview:<#viewControllerToAdd#>.view];
    [<#viewControllerToAdd#> didMoveToParentViewController:self];

willMove is called automatically w/ addChild. willMove 使用addChild 自动调用。 Thanks @iOSSergey谢谢@iOSSergey

When your custom container calls the addChildViewController: method, it automatically calls the willMoveToParentViewController: method of the view controller to be added as a child before adding it.当您的自定义容器调用 addChildViewController: 方法时,它会在添加之前自动调用要添加为子项的视图控制器的 willMoveToParentViewController: 方法。

采用:

[self.view addSubview:obj.view];

Change the frame size of viewcontroller.view.frame , and then add to subview.改变viewcontroller.view.frame的帧大小,然后添加到子视图中。 [viewcontrollerparent.view addSubview:viewcontroller.view]

You may use PopupController for the same one the SDK which shows UIViewController as subview You may check PopupController您可以将 PopupController 用于显示 UIViewController 作为子视图的 SDK 您可以检查PopupController

Here is sample code for the same这是相同的示例代码

popup = PopupController
        .create(self.navigationController!)
        .customize(
            [
                .layout(.center),
                .animation(.fadeIn),
                .backgroundStyle(.blackFilter(alpha: 0.8)),
                .dismissWhenTaps(true),
                .scrollable(true)
            ]
        )
        .didShowHandler { popup in
        }
        .didCloseHandler { popup in
    }
    let container = MTMPlayerAndCardSelectionVC.instance()
    container.closeHandler = {() in
        self.popup.dismiss()
    }

    popup.show(container)

You must set the bounds properties to fit that frame.您必须设置边界属性以适合该框架。 frame its superview properties, and bounds limit the frame in the view itself coordinate system.框架其父视图属性,并且边界限制视图本身坐标系中的框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将子视图添加到UIViewController? - How to add a subview to a UIViewController? 如何在 UIViewController 中将 uitabbarcontroller 添加为子视图 - How to add uitabbarcontroller as a subview in UIViewController 将子视图添加到UIImageView,它是IB中UIViewController的视图出口 - Add a subview to a UIImageView that is the view outlet of a UIViewController in IB 如何将UIViewController添加为subview,使其在标签栏上方可见? - How to add UIViewController as subview , to be visible above tabbar? 如何在UIViewController中引用子视图的子节点 - How to refer to subview's children in a UIViewController UITableViewController的视图作为子视图添加到UIViewController导致应用崩溃 - UITableViewController's view added as subview to UIViewController crashes app 如何将uiviewcontroller的视图作为内容视图添加到uitableviewcell? - How to add uiviewcontroller's view as content view to uitableviewcell? 如何在不使用IB的情况下将TableView Controller作为子视图添加到UIViewController? - How to add a TableView Controller as a subView to a UIViewController without using IB? 仅将子视图添加到当前视图的UINavigationBar - Add subview to current view's UINavigationBar only UIViewController:将self.view设置为我的视图或将我的视图添加为子视图? - UIViewController: set self.view to my view or add my view as subview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM