1
votes

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

3
can you share your code where you are calling this in the parent viewcontroller? Is the custom view xib registered in the parentVC (probably in the viewDidLoad method)?Nitin Alabur
The custom xib isn't registered in the parent.. although neither is my UITableView xib extension and that works.. I'll give this a try tonight, and if it doesn't work I'll share the setup code in the parent. Thanks.TheJeff
The parent is a UIViewController, and a UIViewController doesn't have a way to register a nib that I can find... For instance, this method is part of UITableView, but not UIViewController: [self registerNib:[UINib nibWithNibName:@"CustomView" bundle:nil] forCellReuseIdentifier:@"ReuseId123"];TheJeff
Also the CustomView has a parent UIView within the grandparent UIViewController. The IBOutlet from CustomView skips the parent and connects to the UIViewControllerTheJeff
wondering if the owner should be set to self instead of nil. Have you tried setting it to self in the line loadNibNamed.... ?Nitin Alabur

3 Answers

0
votes

Check your Autolayout in your custom XIB. Something the contain does not show because of it. For example: only center vertically & center horizontally. Try to do different autolayout constraint in your view in custom class.

0
votes

I think something is wrong with layout of your CustomView inside ParentView. Could you please:

  • Check the frame of CustomView when you add it into the ParentView.

  • Check constraints that has CustomView inside ParentView. If you don't have them - add it, constraints will iOS the glue how to layout your CustomView. More information about this you can find here Programmatically Creating Constraints

  • And also you can try to use "Debug View Hierarchy" in Xcode to inspect what is going on on your screen, there you can find your CustomView in the view hierarchy and you will see what is wrong.
0
votes

nibs cannot be nested within the structure.

Instead of:

UIViewController -> UIView -> nibs..

It should be:

UIViewController -> Container View -> UIView with nibs.