如何将数组中的子视图添加到uiview?

时间:2012-05-05 12:04:16

标签: iphone xcode

我的子视图存储在数组中...我想要的是将这些子视图放在我的主视图中......

for(int i=0;i<[imageViewArray count];i++)
    {
        NSLog(@" image array count is %d",[imageViewArray count]);
       // countlayers= countlayers+1;
      //  [canvasView insertSubview:[imageViewArray objectAtIndex:i] atIndex:countlayers];
        [canvasView addSubview:[imageViewArray objectAtIndex:i]];
    }
你能告诉我我做错了吗

2 个答案:

答案 0 :(得分:0)

如果 imageViewArray确实包含初始化的视图,并且这些视图已经具有正确的frame属性,那么您没有做错任何事(即使您的代码更优雅)。

当然,我们还必须确保canvasView确实在屏幕上初始化并可见。尝试将其backgroundColor设置为明显的值:

canvasView.backgroundColor = [UIColor greenColor];

一旦你确定canvasView实际上正在显示,我建议使用快速枚举,这样可以更好地浏览数组,并记录新的子视图以确保它们的框架与你想要的一样。

for (UIView *imageView in imageViewArray) //this is fast enumeration. You can get it through code completion by typing forin
{
    NSLog(@"%@", imageView); //let's take a look at the imageView. What is its frame?
    [canvasView addSubview:imageView];
}

答案 1 :(得分:0)

使用枚举在画布上添加图像使用imageViewArray查看图像的设置框架可能也缺少。

for (UIImageView *imageView in imageViewArray) //enumeration. 
{
    //let's set frame for image view before adding to canvasView
    [imageView setFrame:CGRectMake(x,y,w,h)]; 
    //Also make sure imageView has image or not
    if(imageView.image)
    [canvasView addSubview:imageView];
}