在…的帮助下
@E.Coms
this article
我知道问题出在哪里了。在这里,您可以找到以下步骤,以便为其他人提供答案
如何使用Objective-C可设计的IB\ U在序列图像板中可视化可重用的XIB?
-
创建Xib-c头文件和类文件(
XibViewHolder
)
-
创建Xib文件(
MyView.xib
)
-
将Xib的“文件所有者”类设置为
XibViewHolder公司
-
在任何视图控制器的情节提要上添加
UIView
-
把它调好
UIView视图
XibViewHolder公司
-
把它调好
UIView视图
的接口生成器属性名为Nib Name,它是在Xib holder类中定义的,用于指定要加载的Xib的名称,例如:
MyView
.
这就是
XibViewHolder.h
IB_DESIGNABLE
@interface XibViewHolder : UIView
@property (nonatomic, strong) UIView* contentView;
@property (nonatomic, strong) IBInspectable NSString* nibName;
@end
这就是
XibViewHolder.m
#import "XibViewHolder.h"
@implementation XibViewHolder
- (void)awakeFromNib
{
[super awakeFromNib];
[self xibSetup];
}
- (void)prepareForInterfaceBuilder
{
[super prepareForInterfaceBuilder];
[self xibSetup];
[self.contentView prepareForInterfaceBuilder];
}
- (void)xibSetup
{
if (_nibName == nil) { return; }
UIView* loadedNibView = [self loadViewFromNib];
loadedNibView.frame = self.bounds;
loadedNibView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self addSubview:loadedNibView];
self.contentView = loadedNibView;
}
- (UIView*)loadViewFromNib
{
NSBundle* bundle = [NSBundle bundleForClass:[self class]];
UINib* nib = [UINib nibWithNibName:_nibName bundle:bundle];
return [nib instantiateWithOwner:self options:nil].firstObject;
}
@end