Eren
Eren

Reputation: 57

Objective-C Using custom XiB in another XiB

I've really spent my hours for finding any approach about this problem.

I have;

CustomView.h that extends from UIView and is also IB_DESIGNABLE CustomView.m for implementation of this view with override methods init, initWithCoder and initWithFrame CustomView.xib that bound to CustomView class with all properties And, I have :

  • CustomTableViewCell.h that extends from UITableViewCell
  • CustomTableViewCell.m that implementation of this cell
  • CustomTableViewCell.xib that bound to CustomTableViewCell class with all properties&outlets.

CustomTableViewCell.xib includes CustomView...

There is no any error, but the area of CustomView remains blank.

Upvotes: 3

Views: 1161

Answers (1)

rob180
rob180

Reputation: 901

For view you need to do some work around.

Create a property and link your view in the XIB to it;

@property (strong, nonatomic) IBOutlet UIView *xibView;

Then have something like this:

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

- (instancetype)init{
    self = [super init];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit{
    //this will work if the view has the same name as the XIB
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
    self.xibView.frame = self.bounds;
    [self addSubview:self.xibView];
    self.xibView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

}

Basically you have to load the XIB manually and add it to your custom view.

Upvotes: 2

Related Questions