Reputation:
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
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.
Additional Info
I'm creating a UIView
dynamically and also a few UIImageView
which I was hoping that [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).
Upvotes: 0
Views: 248
Reputation: 5149
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.
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.
Upvotes: 1
Reputation: 8576
If I understand your question correctly, using a UIView
with UIImageView
subviews is the right approach, IMO. What you said also should work fine - creating a UIView
, adding UIImageView
subviews, then moving the parent view and changing its visibility, etc.
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
Upvotes: 1