多个UIPickerViews - 没有.xib
问题描述:
我有一个视图。我希望能够把1-3 UIPickerView
放入其中。 UIPickerView
s的数量由变量myVariable
的值确定。每个UIPickerView
是不同的。 myVariable
的某些值意味着视图中只有一个UIPickerView
,但其中一些意味着我将不得不在视图中放置2或3,并且它将垂直放置(因此一个UIPickerView
正好在另一个的上方)。现在,每一个情况下myVariable
意味着只需要1 UIPickerView
,我把他们的观点是这样的:多个UIPickerViews - 没有.xib
if (myVariable == kNeedsOnlyOnePicker)
{
myPicker = [[UIPickerView alloc] initWithFrame:self.view.frame];
[myPicker setDelegate:self];
[myPicker setDataSource:self];
myPicker.showsSelectionIndicator = YES;
[self.view addSubview:myPicker];
}
不过,我只是不知道如何告诉代码是这样的:
if (myVariable == kNeedsTwoPickers)
{
// put myPicker and myOtherPicker in self.view,
// and put myPicker above myOtherPicker.
}
我有一种预感,我将不能使用的.xib做这样的事情,因为我将无法设置不同的布局(例如装载机1以上摘取机2,选择器2在选取器3之上,仅选取器3)。
TL; DR我如何以编程方式说,“这个子视图会超出这个子视图”或“这个选择器会超出这个其他选择器”?
具体方法/实例和/或一般想法会很好。
谢谢!
答
我发现我可以为子视图设置框架并计算其坐标。
- (void)layout
{
for (int i= 0; i < [self.view.subviews count]; i++)
{
// calculate x, y, width, and height
UIView *subview = [self.view.subviews objectAtIndex:i];
subview.frame = CGRectMake(x, y, width, height);
}
}