ios - 多个 UIPickerViews - 没有 .xib

标签 ios objective-c uiview uipickerview

我有一个观点。我希望能够在其中放入 1-3 个 UIPickerViewUIPickerView 的数量由变量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/

相关文章:

ios - 将触摸从 uiview 转移到 uiscrollview

iphone - iOS4调用ImageNamed : still leak or cause memory issue?

swift - 如何在延迟滚动时更改 UIView 大小

ios - 将 Facebook 图像设置为按钮背景

ios - 从 alamofire swift 从 tableview 单元格中的 url 加载图像

objective-c - Objective-C - 如何以编程方式停止执行以进行调试,同时允许继续?

objective-c - 我该如何修改以及修改什么类/文件才能以编程方式插入 Cocoa NSButton?

iphone - UIView的setNeedsLayout、layoutIfNeeded和layoutSubviews之间有什么关系?

ios - NSXML 使用 Swift 将损坏的字符串解析为数组

ios - 如何在 iPhone 上创建圆角 UILabel?