Coshak
Coshak

Reputation: 65

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

I am trying to add a custom UIView that I created in XIB, to my view controller in my main.storyboard as a subview. How can I do this?

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

With the press of a button i want to have the custom UIView added to the UIScrollView as 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 UIViews programmatically, but have yet to find a way to just load the UIView from 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 UIView in 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 UIScrollView has a numerous subviews of UIViews.

I was hoping I could create numerous instances of MatchView and add them all to matchScrollView as subviews.

Upvotes: 3

Views: 7299

Answers (1)

Pauls
Pauls

Reputation: 2636

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.

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:

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

B) Go to your GlobalView.xib, drag the subview and make it of class 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).

- (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;
}

Upvotes: 3

Related Questions