【问题标题】:Add the same UIView in another UIView multiple times在另一个 UIView 中多次添加相同的 UIView
【发布时间】:2012-01-25 15:55:40
【问题描述】:

是否可以将同一个视图多次添加到另一个视图中。我正在尝试使用addSubview 执行此操作,但最终结果是我添加的视图仅位于设置的最后一个位置。

while (count <= [_testItemArray count]) {
    [self.parentView addSubview:self.childrenView];
    yPosition = count * 37;
    [self.childrenView setFrame:CGRectMake(0, yPosition, 0, 0)];
    count++;
}

我正在构建的应用程序在运行时检索 childrenView 中的信息。 childrenView 的数量每次都可能不同。

【问题讨论】:

    标签: iphone objective-c ios uiview uikit


    【解决方案1】:

    您只能添加一次UIView 的实例,但如果您将UIView 子类化并为您的自定义视图创建一个类,您可以根据需要多次实例化它。

    我的建议是创建一个类MyCustomView,它是UIView 的子类。然后您的循环将如下所示:

    while (count <= [_testItemArray count]) {
        MyCustomView *customView = [[MyCustomView alloc] init];
        [self.parentView addSubview:customView;
        [customView release];
        yPosition = count * 37;
        [customView setFrame:CGRectMake(0, yPosition, 0, 0)];
        count++;
    }
    

    【讨论】:

    • 感谢您的快速回复。我刚刚编辑了我的问题以反映我也看到了 childrenView 的框架。您发布的代码段是否会更改为 [self.customView setFrame:CGRectMake(0, yPosition, 0, 0)]; ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多