简体   繁体   中英

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 - IE (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]; [self addSubview: self.image1]; [self bringSubviewToFront: self.image1]; [self.menuImage setHidden:image1]; -- with no luck.

Figure 1: (Actual result) 实际的

Figure 2 (Expected.. with red background) 预期的

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.

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.

nibs cannot be nested within the structure.

Instead of:

UIViewController -> UIView -> nibs..

It should be:

UIViewController -> Container View -> UIView with nibs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM