Objective-C 在另一个 XiB 中使用自定义 XiB

Objective-C Using custom XiB in another XiB

我真的花了很多时间来寻找解决这个问题的方法。

我有;

CustomView.hUIView 延伸并且也是 IB_DESIGNABLE CustomView.m 用于使用覆盖方法 initinitWithCoderinitWithFrame 实现此视图 CustomView.xib 绑定到具有所有属性的 CustomView class 而且,我有:

  • 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 包括 CustomView...

没有任何错误,但是CustomView的区域还是空白。

为了查看,您需要做一些工作。

XIB 中创建一个 属性 和 link 视图;

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

然后有这样的东西:

- (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;

}

基本上您必须手动加载 XIB 并将其添加到您的自定义视图。