Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I follow I think you will need to:</p> <p>Create a <code>UIView</code> sub class with associated xib</p> <pre><code>ExampleView.h ExampleView.m ExampleView.xib </code></pre> <p>Set up a property to contain the View hierarchy (everything you want to be hooked up in Interface builder)</p> <pre><code>ExampleView.h @property (nonatomic, retain) IBOutlet UIView *exampleView; ExampleView.m @synthesize exampleView = _exampleView; </code></pre> <p>In the <code>ExampleView.m</code> you need to add:</p> <pre><code>- (void)awakeFromNib { [super awakeFromNib]; [[NSBundle mainBundle] loadNibNamed:@"ExampleView" owner:self options:nil]; [self addSubview:self.exampleView]; } </code></pre> <p>You use awake from nib so you can use this sub class in Interface Builder. If you also want to instantiate it in code you need to hook up the xib in the init method. In this case I would probably extract the nib loading like so:</p> <pre><code>- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self loadNibIntoView]; } return self; } - (void)awakeFromNib { [super awakeFromNib]; [self loadNibIntoView]; } - (void)loadNibIntoView { [[NSBundle mainBundle] loadNibNamed:@"ExampleView" owner:self options:nil]; [self addSubview:self.exampleView]; } </code></pre> <p>In the ExampleView.xib file make sure to hook up your <code>IBOutlet UIView *exampleView</code> to the base view.</p> <p>Now in any xib file where you want to use your custom view simply drag it into your window and change the class to your subclass. This is how I got it working I look forward to some people suggesting improvements.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload