我真的花了很多时间来寻找解决这个问题的方法。
我有;
CustomView.h 从 UIView
扩展并且也是 IB_DESIGNABLE
CustomView.m 用于使用覆盖方法 init
、initWithCoder
和 initWithFrame
实现此 View
CustomView.xib 绑定(bind)到具有所有属性的 CustomView 类
而且,我有:
- 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
中的 View 链接到它;
@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
并将其添加到您的自定义 View 中。
关于ios - Objective-C 在另一个 XiB 中使用自定义 XiB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34022382/