xcode 如何将 XIB 文件中的 UIView 作为子视图添加到另一个 Xib 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29630436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:59:37  来源:igfitidea点击:

How to add an UIView from a XIB file as a subview to another Xib file

iosobjective-cxcodeuiviewxib

提问by Coshak

I am trying to add a custom UIViewthat I created in XIB, to my view controller in my main.storyboardas a subview. How can I do this?

我正在尝试将UIView我在 XIB 中创建的自定义添加到我的视图控制器中main.storyboard作为子视图。我怎样才能做到这一点?

matchScrollView(tag 1) is the UIScrollViewin view controller in main.storyboard, while matchView(tag 2) is the custom UIViewI created in another XIB file.

matchScrollView(tag 1) 是UIScrollViewmain.storyboard 中的视图控制器,而matchView(tag 2) 是UIView我在另一个 XIB 文件中创建的自定义。

With the press of a button i want to have the custom UIViewadded to the UIScrollViewas a subview. But how can i actually make it show up on display? I guess i have yet to alloc and init it, along with indicate position and such, but how can i do that? I tried different ways without success. I can create UIViewsprogrammatically, but have yet to find a way to just load the UIViewfrom XIB.

按下按钮,我希望将自定义UIView添加到UIScrollView子视图中。但是我怎样才能让它显示在显示器上呢?我想我还没有分配和初始化它,以及指示位置等,但我该怎么做?我尝试了不同的方法但没有成功。我可以以UIViews编程方式创建,但还没有找到一种方法来UIView从 XIB加载。

-(IBAction) buttonTapped:(id)sender {

    UIScrollView *matchScrollView = (UIScrollView *) [self.view viewWithTag:1];
    UIView *matchView = (UIView *) [self.view viewWithTag:2];
    [matchScrollView addSubview:matchView];

}

The reason that I am creating my custom UIViewin another XIB file instead of directly implementing it on my main.storyboard view controller, is because I want to re-use the same view multiple times. So the UIScrollViewhas a numerous subviews of UIViews.

UIView在另一个 XIB 文件中创建我的自定义而不是直接在我的 main.storyboard 视图控制器上实现它的原因是因为我想多次重用同一个视图。所以UIScrollView有很多子视图UIViews

I was hoping I could create numerous instances of MatchViewand add them all to matchScrollViewas subviews.

我希望我可以创建许多实例MatchView并将它们全部添加matchScrollView为子视图。

采纳答案by Pauls

The issue you are having is completely normal. The way Apple designed it doesn't allow to reuse custom views with their own xib into other xibs.

您遇到的问题是完全正常的。Apple 设计它的方式不允许将带有自己的 xib 的自定义视图重用于其他 xib。

Lets say you have a custom view named HeaderView with a custom xib named HeaderView.xib. And lets say you want to be able to, in another xib named GlobalView.xib, drag a subview and specify its class to be of type HeaderView expecting it to load that view from HeaderView.xib and insert it inplace. You can do it like this:

假设您有一个名为 HeaderView 的自定义视图和一个名为 HeaderView.xib 的自定义 xib。假设您希望能够在另一个名为 GlobalView.xib 的 xib 中拖动一个子视图并将其类指定为 HeaderView 类型,期望它从 HeaderView.xib 加载该视图并将其插入到位。你可以这样做:

A) Make sure File's Owner in HeaderView.xib is set to be HeaderView class.

A) 确保 HeaderView.xib 中的 File's Owner 设置为 HeaderView 类。

B) Go to your GlobalView.xib, drag the subview and make it of class HeaderView.

B) 转到您的 GlobalView.xib,拖动子视图并使其成为 HeaderView 类。

C) In HeaverView.m implement initWithCoder, if after loading the view there aren't subviews means it got loaded from GlobalView, then load it manually from the correct nib, connect the IBOutlets and set the frame and autoresizingmasks if you want to use the GlobalView's frame (this is usually what you want).

C) 在 HeaverView.m 中实现 initWithCoder,如果加载视图后没有子视图意味着它是从 GlobalView 加载的,然后从正确的笔尖手动加载它,连接 IBOutlets 并设置框架和 autoresizingmasks 如果你想使用GlobalView 的框架(这通常是您想要的)。

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self.subviews.count == 0) { //means view got loaded from GlobalView.xib or other external nib, cause there aren't any subviews
        HeaverView *viewFromNib = [[NSBundle mainBundle] loadNibNamed:@"HeaverView" owner:self options:nil].firstObject;
        //Now connect IBOutlets
        self.myLabel1 = viewFromNib.myLabel1;
        self.myLabel2 = viewFromNib.myLabel2;
        self.myLabel3 = viewFromNib.myLabel3;
        [viewFromNib setFrame:self.bounds];
        [viewFromNib setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
        [self addSubview:viewFromNib];
    }

    return self;
}