我有一个观点。我希望能够在其中放入 1-3 个 UIPickerView
。 UIPickerView
的数量由变量myVariable
的值决定。每个 UIPickerView
都是不同的。 myVariable
的某些值意味着 View 中只有一个 UIPickerView
,但有些值意味着我必须在 View 中放置 2 或 3 个,并且它将垂直布局(因此一个 UIPickerView
位于另一个的正上方)。现在,在 myVariable
意味着它只需要 1 个 UIPickerView
的情况下,我将它们放在这样的 View 中:
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 如何以编程方式说“此 subview 将位于其他 subview 之上”或“此选择器将位于其他选择器之上”?
具体方法/示例和/或一般想法会很棒。
谢谢!
最佳答案
我发现我可以为 subview 设置框架并计算它的坐标。
- (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);
}
}
关于ios - 多个 UIPickerViews - 没有 .xib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11908797/