Search code examples
iosobjective-cinterface-builderxib

UIView custom xib doesn't show up


I'm having a problem that doesn't seem to have an obvious solution. I've searched around and I've been through all common answers that I could find.

My custom xib views don't show up on the app when I launch. The background is clear, and this xib has 5 image views as you can see below which aren't set to hidden.

The class has about 5 delegates as well which I set from a caller when the delegates have been initialized. The caller of initWithDelegates: is the parent UIViewController that displays this xib.

CustomView.h

@interface CustomView : UIView<SomeProtocol>

// UI Items - THESE AREN'T SHOWING UP
@property (strong, nonatomic) IBOutlet UIImageView *image1;
@property (strong, nonatomic) IBOutlet UIImageView *image2;
@property (strong, nonatomic) IBOutlet UIImageView *image3;
@property (strong, nonatomic) IBOutlet UIImageView *image4;
@property (strong, nonatomic) IBOutlet UILabel *label;

@property (strong, nonatomic) IBOutlet UIStackView *centerStackView;

- (id)initWithDelegates:(UIViewController*)delegate1 withDelegate2:(NSObject<Delegate2>*)delegate2 withDelegate3:(NSObject<Delegate3>*)delegate3 withDelegate4:(UIViewController<Delegate4>*)delegate4
    withDelegate5:(NSObject<Delegate5>*)delegate5;

@end

CustomView.m

@implementation CustomView

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (id)initWithDelegates:(UIViewController*)delegate1 withDelegate2:(NSObject<Delegate2>*)delegate2 withDelegate3:(NSObject<Delegate3>*)delegate3 withDelegate4:(UIViewController<Delegate4>*)delegate4
    withDelegate5:(NSObject<Delegate5>*)delegate5
{

    NSArray * arr =[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:nil options:nil];
    self = [arr firstObject];


    self.delegate1 = delegate1;
    self.delegate2 = delegate2;
    self.delegate3 = delegate3;
    self.delegate4 = delegate4;
    self.delegate5 = delegate5;

    [self setLoopImage];

    [self layoutIfNeeded];

    return self;
}
@end

What else I've verified:

  1. I made sure that the xib file is properly selected under "Custom Class" in the interface builder.
  2. I've walked through the code to make sure there are no crashes or obvious issues.
  3. The xib doesn't appear to have any obvious issues in interface builder such as being set to HIDDEN.
  4. The 5 IBOutlet ImageViews get a memory address and aren't nil.

Any help is appreciated!

UPDATE:

I believe this has something to do with putting the nibs in a nested UIView. In the interface builder, the order is UIViewController -> UIView -> nibs

There doesn't seem to be a method to register nibs from the parent UIViewController as there are in classes like UITableView.

I've also tried:

  1. Setting the background color in the xib to make sure it shows up. It doesn't. However if I set the background color to red in the parent where I added the view in interface builder, it turns red. (See Figure 1), and my xib file is shown in Figure 2
  2. Over-rode initWithFrame to make sure its being called. It is called.
  3. Thinking that the view may be instantiated twice, one via autolayout and one via my custom init method, I tried making the (id)initWithDelegates method not init or register the nib in case that was duplicating the object. I did this by removing 1) return self, 2) the lines that register the nib, and 3) making the method prototype return void - I.E. (void)initWithDelegates - This didn't work either.
  4. I tried these lines within initWithDelegates : [self addSubview: self.image1]; [self bringSubviewToFront: self.image1]; [self.menuImage setHidden:image1]; -- with no luck.

Figure 1: (Actual result) Actual

Figure 2 (Expected.. with red background) Expected


Solution

  • nibs cannot be nested within the structure.

    Instead of:

    UIViewController -> UIView -> nibs..

    It should be:

    UIViewController -> Container View -> UIView with nibs.