如何在UIView中加载xib文件

我一直在到处搜寻,至今没有任何东西为我工作。

基本上我想要一个名为rootView.xib的.xib文件,并且在其中我想要一个只占用屏幕一半的UIView(让它称为containerView)(因此会有常规视图和新视图)。 然后我想要一个名为firstView.xib的不同的.xib文件并将其加载到containerView中。 所以我可以在firstView.xib和一堆不同的东西在rootView.xib中加入一堆东西,并在rootView.xib的containerView中加载我的firstView.xib,但由于它只占用了屏幕的一半,所以仍然可以看到在rootView.xib上的东西


要以编程方式从xib文件获取对象,可以使用: [[NSBundle mainBundle] loadNibNamed:@"MyXibName" owner:self options:nil]返回xib中顶级对象的数组。

所以,你可以做这样的事情:

UIView *rootView = [[[NSBundle mainBundle] loadNibNamed:@"MyRootView" owner:self options:nil] objectAtIndex:0];
UIView *containerView = [[[NSBundle mainBundle] loadNibNamed:@"MyContainerView" owner:self options:nil] lastObject];
[rootView addSubview:containerView];
[self.view addSubview:rootView];

我在github上创建了一个样例项目,用于从另一个.xib文件中的.xib文件加载UIView。 或者你可以通过编程来完成。

这对于要在不同的UIViewController对象上重用的小部件很有用。

  • 新方法:https://github.com/PaulSolt/CustomUIView
  • 原始方法:https://github.com/PaulSolt/CompositeXib
  • 从.xib文件加载自定义UIView


    你可以尝试:

    UIView *firstViewUIView = [[[NSBundle mainBundle] loadNibNamed:@"firstView" owner:self options:nil] firstObject];
    [self.view.containerView addSubview:firstViewUIView];
    
    链接地址: http://www.djcxy.com/p/87693.html

    上一篇: How to load a xib file in a UIView

    下一篇: Access Container View Controller from Parent iOS