多个UIPickerViews - 没有.xib

时间:2012-08-10 20:28:00

标签: ios objective-c uiview uipickerview

我有一个观点。我希望能够将1-3 UIPickerView放入其中。 UIPickerView s的数量由变量myVariable的值确定。每个UIPickerView都不同。 myVariable的某些值意味着视图中只有一个UIPickerView,但有些意味着我必须在视图中放置2或3,并且它将垂直布局(所以一个UIPickerView直接位于另一个之上。现在,myVariable意味着它只需要1 UIPickerView的每个案例,我都将它们放在这样的视图中:

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来做这样的事情,因为我将无法设置不同的布局(例如拾取器2上方的拾取器1,拾取器3上方的拾取器2,只有选择器3)。

TL; DR我如何以编程方式说“这个子视图将超越这个其他子视图”或“这个选择器会超过这个其他选择器”?

具体方法/示例和/或一般想法会很棒。

谢谢!

1 个答案:

答案 0 :(得分:1)

我发现我可以为子视图设置框架并计算其坐标。

- (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);
    }
}
相关问题