我知道这个问题以前曾被问过,但答案是矛盾的,我很困惑,所以请不要攻击我。
我想要一个可重复使用的UIView
整个我的应用程序的子类。我想使用 nib 文件来描述界面。
现在假设它是一个加载指示器视图,其中有一个活动指示器。我希望在某些事件中实例化此视图并以动画方式呈现到视图控制器的视图中。我可以以编程方式描述视图的界面,以编程方式创建元素并在 init 方法中设置它们的框架等,这是没有问题的。
我怎样才能使用笔尖做到这一点?保持界面生成器中给定的尺寸,而无需设置框架。
我已经设法这样做了,但我确信这是错误的(这只是一个带有选择器的视图):
- (id)initWithDataSource:(NSDictionary *)dataSource {
self = [super init];
if (self){
self = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"%@", [self class]] owner:self options:nil] objectAtIndex:0];
self.pickerViewData = dataSource;
[self configurePickerView];
}
return self;
}
但我正在覆盖 self,当我实例化它时:
FSASelectView *selectView = [[FSASelectView alloc] initWithDataSource:selectViewDictionary];
selectView.delegate = self;
selectView.frame = CGRectMake(0, self.view.bottom + 50, [FSASelectView width], [FSASelectView height]);
我必须手动设置框架而不是从 IB 中获取它。
编辑:我想在视图控制器中创建此自定义视图,并有权控制视图的元素。我不需要新的视图控制器。
Thanks
编辑:我不知道这是否是最佳实践,我确信不是,但这就是我的做法:
FSASelectView *selectView = [[[NSBundle mainBundle] loadNibNamed:[NSString stringWithFormat:@"%@",[FSASelectView class]] owner:self options:nil] objectAtIndex:0];
selectView.delegate = self;
[selectView configurePickerViewWithData:ds];
selectView.frame = CGRectMake(0, self.view.bottom + 50, selectView.width, selectView.height);
selectView.alpha = 0.9;
[self.view addSubview:selectView];
[UIView animateWithDuration: 0.25 delay: 0 options:UIViewAnimationOptionAllowUserInteraction |UIViewAnimationOptionCurveEaseInOut animations:^{
selectView.frame = CGRectMake(0, self.view.bottom - selectView.height, selectView.width, selectView.height);
selectView.alpha = 1;
} completion:^(BOOL finished) {
}];
仍然需要正确的练习
这应该使用视图控制器和带有 nib 名称的 init 来完成吗?我应该在代码中的某些 UIView 初始化方法中设置笔尖吗?或者我所做的一切都好吗?