我知道以前有人问过这个问题,但答案是矛盾的,我很困惑,所以请不要喷我。
我想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;
}
但我正在覆盖自我,当我实例化它时:
FSASelectView *selectView = [[FSASelectView alloc] initWithDataSource:selectViewDictionary];
selectView.delegate = self;
selectView.frame = CGRectMake(0, self.view.bottom + 50, [FSASelectView width], [FSASelectView height]);
我必须手动设置框架,而不是从 IB 获取框架。
编辑:我想在视图控制器中创建这个自定义视图,并有权控制视图的元素。我不想要一个新的视图控制器。
谢谢
编辑:我不知道这是否是最佳做法,我确定不是,但这就是我的做法:
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 初始化方法中设置笔尖吗?或者我做的还好吗?