我在使用接口构建器创建的UIView内部有一个UIViewController。我想在我的应用程序中共享并重用这个UIView。UIView将始终具有相同的设计,并且只会更改其数据。
我已经知道的是:
UIView,并将其重新添加到每个UIViewController中--这不是我的解决方案,我想使用接口构建器创建UIView。UIView难道没有更好的解决方案吗?也许不用xib?我对上面的解决方案投了赞成票,但感觉有点烦躁。谢谢
发布于 2013-07-05 08:56:11
您不可能使用故事板重用UIView,您需要做的是子类UIView,我们将它命名为CustomView,在(void)awakeFromNib方法中,您可以使用addSubViews进行设计,比如,将背景颜色设置为UIView。
您可以在故事板中使用这个SubClass,重用它来为任意UIViewController呈现相同的UIView,您只需将类从UIView设置为UIViewController的IdentityInspector of UIView中的CustomView即可。
示例自定义UIView类
#import "CustomView.h"
@implementation CustomView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
UIImage *bgImage = [[UIImage imageNamed:@"bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0)];
UIImageView *bgImageView;
bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, viewHeight(self))];
[bgImageView setImage:bgImage];
[self addSubview:bgImageView];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end发布于 2013-07-26 23:39:16
这是不可能的。storyBoard由xib(nib)文件组成。您在storyBoard中生成的自定义视图将是xib文件。您不能通过代码“不”使用xib文件或"not“,因为这是您仅有的两个选项。使用外部xib,如您提供的示例中所示,将是您的最佳选择。
https://stackoverflow.com/questions/17484225
复制相似问题