[英]UIView as parent for UIImageView
What I'm trying to achieve 我正在努力实现的目标
nutshell: 简而言之:
use UIView as a container that will move it's "sub-views" when it is moved and also automagically set their visibility when it's visibility changes 将UIView用作容器,当其移动时将移动其“子视图”,并在其可见性更改时自动设置其可见性
long version: 长版:
I'd like to have multiple UIImageView within a UIView, whenever UIView is visible, all UIImageView
are visible, when UIView
is hidden, all UIImageView
are hidden, when UIView
changes position, all UIImageView
will preserve position within their UIView
parent. 我想在一个UIView中有多个UIImageView,每当可见UIView时,所有UIImageView
都可见,当UIView
隐藏时,所有UIImageView
被隐藏,当UIView
更改位置时,所有UIImageView
将在其UIView
父级中保留位置。
Additional Info 附加信息
I'm creating a UIView
dynamically and also a few UIImageView
which I was hoping that [UIView addSubview:UIImageViewInstance];
我创建一个UIView
动态也有几个UIImageView
这我希望[UIView addSubview:UIImageViewInstance];
would take care of that, unfortunately, it doesn't. 会解决这个问题,不幸的是,事实并非如此。
I'm not using the interface builder! 我没有使用界面生成器!
Question 题
how can I achieve above said? 我如何实现以上所说的? If you believe that UIView
is not the correct way to solve my problem, feel free to present other option(s). 如果您认为UIView
不是解决我的问题的正确方法,请随时提出其他选择。
If I understand your question correctly, using a UIView
with UIImageView
subviews is the right approach, IMO. 如果我正确理解了您的问题,那么将UIView
与UIImageView
子视图一起使用是正确的方法。 What you said also should work fine - creating a UIView
, adding UIImageView
subviews, then moving the parent view and changing its visibility, etc. 您所说的内容也应该可以正常工作-创建UIView
,添加UIImageView
子视图,然后移动父视图并更改其可见性等。
I would recommend using something like this: 我建议使用这样的东西:
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
Well, the approach that you are taking is just fine. 好吧,您采用的方法很好。 I'll just write down the code for your reference. 我将写下代码以供您参考。 I am assuming that you are working with an instance of UIViewController
and you are adding subviews to it. 我假设您正在使用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];
This should take care of your issue. 这应该解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.