【问题标题】:Objective-C Using custom XiB in another XiBObjective-C 在另一个 XiB 中使用自定义 XiB
【发布时间】:2016-03-05 11:49:16
【问题描述】:
我真的花了很多时间来寻找解决这个问题的任何方法。
我有;
CustomView.h 扩展自 UIView 并且也是 IB_DESIGNABLE
CustomView.m 用于使用覆盖方法init、initWithCoder 和initWithFrame 实现此视图
CustomView.xib 绑定到具有所有属性的 CustomView 类
而且,我有:
- 从 UITableViewCell 扩展的 CustomTableViewCell.h
- CustomTableViewCell.m 表示此单元格的实现
- CustomTableViewCell.xib 绑定到具有所有属性和插座的 CustomTableViewCell 类。
CustomTableViewCell.xib 包括 CustomView...
没有任何错误,但CustomView的区域保持空白。
【问题讨论】:
标签:
ios
objective-c
xcode
uiview
xib
【解决方案1】:
对于视图,您需要做一些工作。
创建一个属性并将XIB 中的视图链接到它;
@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 并将其添加到您的自定义视图中。