Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8700475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:10:37+00:00 2026-06-13T02:10:37+00:00

I externalized a component in a XIB file so I can reuse it in

  • 0

I externalized a component in a XIB file so I can reuse it in several different views.
Now in my view controller I load the XIB-based UIView and add it to the current view through :

NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"ThumbsPreviewView" owner:self options:nil];
_likePreview = [views objectAtIndex:0];
[self.view addSubview:_likePreview];

At first, I thought that it was not working… but actually it works but only put the root view (i.e. the container of all my graphical components, which is transparent) to my view.

So I need to put this kind of code (for every sub component) to make it work :

[self.view addSubview:_likePreview];
NSArray *subviews = _likePreview.subviews;
for(UIView * subview in subviews) {
    [self.view addSubview:subview];
}

This is very counter-intuitive, and moreover I think it does not create the view hierarchy that I would like.

I am sure there is a solution because I did the same kind of thing in a UITableViewController to load a custom table header from a XIB. Using the exact same XIB file in this situation loads everything properly. Here is the code :

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    switch(section) {
        case 0: {
            NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"ThumbsPreviewView" owner:self options:nil];
            _likePreview = [views objectAtIndex:0];
            return _likePreview;
        }
    }
    return [super tableView:tableView viewForHeaderInSection:section];
}

With the above code, everything is fine, I got the whole view (= root view container with all its content) as the header of my table section.

So the question is : what am I doing wrong in my first example ? How can I add all content (container + all content) to my parent UIView as a proper UIView hierarchy ?

EDIT : I need to precise that everything is organized in my XIB file in a single root UIView, as follows (I cannot post image) :

* Thumbs preview view
|_ Button
|_ Image View
|_ Image View
|_ Image View
|_ Image View
|_ Button
|_ Button
|_ Button
|_ Label

Thank you very much for your help !

Christophe

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:10:39+00:00Added an answer on June 13, 2026 at 2:10 am

    One way to load your component is to store your components into a top UIView object (s. attached screenshot).


    (source: mobypicture.com)

    You can then load your nib as usual and use -addSubview: to add your components to the view hierarchy.

    EDIT:

    Loading the nib with

    UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"testView" owner:self options:nil] objectAtIndex:0];
    

    and adding the view to the view hierarchy with

    [self.view addSubview:view];
    

    leads to the following view hiearchy:

    (lldb) po [[self view] recursiveDescription]
    (id) $1 = 0x088487b0 <UIView: 0x89595b0; frame = (0 0; 320 548); autoresize = W+H; layer = <CALayer: 0x8958b30>>
       | <UIView: 0x89755c0; frame = (0 0; 320 200); autoresize = RM+BM; layer = <CALayer: 0x8975080>>
       |    | <UIView: 0x8975620; frame = (200 20; 100 30); autoresize = W+H; layer = <CALayer: 0x8975680>>
       |    | <UIView: 0x8975700; frame = (20 20; 100 30); autoresize = W+H; layer = <CALayer: 0x8974ea0>>
       |    | <UIRoundedRectButton: 0x8975790; frame = (20 58; 73 44); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x8975890>>
       |    |    | <UIGroupTableViewCellBackground: 0x8976030; frame = (0 0; 73 44); userInteractionEnabled = NO; layer = <CALayer: 0x8976100>>
       |    |    | <UIImageView: 0x89768d0; frame = (1 1; 71 43); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x89770f0>>
       |    |    | <UIButtonLabel: 0x8977af0; frame = (36 22; 0 0); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8977be0>>
       |    | <UIRoundedRectButton: 0x8978990; frame = (227 58; 73 44); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x8978a60>>
       |    |    | <UIGroupTableViewCellBackground: 0x8978a90; frame = (0 0; 73 44); userInteractionEnabled = NO; layer = <CALayer: 0x8978b10>>
       |    |    | <UIImageView: 0x8978b80; frame = (1 1; 71 43); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8979230>>
       |    |    | <UIButtonLabel: 0x8978be0; frame = (36 22; 0 0); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x8978cd0>>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a localized spring mvc based web application, that has an externalized messages
I am using externalized config section for appsettings, to be able to have different
I want to externalize generic parts of my large build file in order to
i have the following xml. legacy: xml (can't change because it is externalize). <bean
I'm working on a Spring 2.5 project where four different webapps have duplicate JSPs
Here is my problem: I'm developing a system that can be polled to retrieve
I am working on a web app with a visual component which needs to
In a current web project, I'm using several jQuery plugins and initializing them just
Whenever I deploy a production application that has an error with an externalized configuration
The value proposition of externalizing identity is starting to increase where many sites now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.