Ios UIView作为UIImageView的父对象

Ios UIView作为UIImageView的父对象,ios,objective-c,uiview,uiimageview,Ios,Objective C,Uiview,Uiimageview,我正在努力实现的目标 简而言之: 将UIView用作一个容器,当它被移动时,它将移动它的“子视图”,并且当它的可见性发生变化时,也会自动设置它们的可见性 长版本: 我希望在一个UIView中有多个UIImageView,每当UIView可见时,所有UIImageView都可见,当UIView隐藏时,所有UIImageView都隐藏,当UIView更改位置时,所有UIImageView都将保留其UIView父视图中的位置 其他信息 我正在动态地创建一个UIView,还有一些UIImageView,

我正在努力实现的目标

简而言之:

将UIView用作一个容器,当它被移动时,它将移动它的“子视图”,并且当它的可见性发生变化时,也会自动设置它们的可见性

长版本:

我希望在一个UIView中有多个UIImageView,每当UIView可见时,所有
UIImageView
都可见,当
UIView
隐藏时,所有
UIImageView
都隐藏,当
UIView
更改位置时,所有
UIImageView
都将保留其
UIView
父视图中的位置

其他信息

我正在动态地创建一个
UIView
,还有一些
UIImageView
,我希望
[UIView addSubview:UIImageView实例]
会解决这个问题,不幸的是,它没有

我不是在使用界面生成器

问题


我怎样才能实现上述目标?如果您认为
UIView
不是解决我问题的正确方法,请随意提出其他选项。

如果我正确理解您的问题,在
UIImageView
子视图中使用
UIView
是正确的方法,依我看。您所说的也应该很好-创建
UIView
,添加
UIImageView
子视图,然后移动父视图并更改其可见性,等等

我建议您使用以下方法:

UIView *parent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
[self.view addSubview:parent];

//Make a horizontal row of 5 images
for (int i = 0; i < 5; i++) {

    UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(i * 25, 0, 20, 20)];
    [imgV setImage:[UIImage imageNamed:@"yourimage.png"]];
    [parent addSubview:imgV];
}

//Then you should be able to do things like this and it will apply to the subviews as well
[parent setCenter:self.view.center];
[parent setAlpha:0.8];
//Etc
UIView*parent=[[UIView alloc]initWithFrame:CGRectMake(0,0300300)];
[self.view addSubview:parent];
//将5个图像水平排成一行
对于(int i=0;i<5;i++){
UIImageView*imgV=[[UIImageView alloc]initWithFrame:CGRectMake(i*25,0,20,20)];
[imgV setImage:[UIImage ImageName:@“yourimage.png”];
[父添加子视图:imgV];
}
//然后您应该能够执行类似的操作,并且它也将应用于子视图
[父集合中心:self.view.center];
[父集合α:0.8];
//等

好吧,你采取的方法很好。我只写下代码供你参考。我假设您正在使用
UIViewController
的一个实例,并且正在向其中添加子视图

UIView *parentView = [[UIView alloc]initWithFrame:
                                   CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
UIImageView *childImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,50,50)];
[parentView addSubview:childImageView];

这应该可以解决您的问题。

谢谢您,先生,我刚刚注意到我已经将UIImageView添加到“覆盖”UIView,基本上,层次结构是UIView->UIView->2 UIImageView--失去焦点并添加到错误的视图>。