Resizing subview with superview's frame change - ios

I have a UIImageView aSuper; This view is changing its sizes when i flip it i.e. it is taking sizes of next frame and next frame and so on.
Now this UIImageView contains a subview which is also imageview. I will call it bSubView.
Now i want to resize this bSubView every time its parent's view frame changes.
Code is :- Here invisibleView is subview and viewHolder is parent view. parent view is changing right but problem is with subview.
UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageString1]]];
UIImageView *viewHolder=[[UIImageView alloc] initWithFrame:CGRectMake(510, 200, 20, 20)];
invisibleView=[[UIImageView alloc] init];
invisibleView.image=image;
invisibleView.frame=viewHolder.bounds;
viewHolder.autoresizesSubviews=YES;
[ invisibleView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
invisibleView.contentMode=UIViewContentModeCenter;
viewHolder.image=[UIImage imageNamed:#"EmptyPlanet"];
[viewHolder addSubview:invisibleView];
[self.view addSubview:viewHolder];
Many Thanks in advance.

you can use the inherited method -(void)layoutsubviews
This method is called on the super view when its frame changes. In that method you can configure your subviews
Another option is to use autolayout when configuring the subviews.

Related

How to use UIScrollView

I am trying to achieve something similar to the photo options bar in VSCO Cam. I tried setting it up with an image, just to get an idea on how to do it, but its not working.
I think one of the problems could be with the fact that the UIScrollView should be horizontal.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.scrollView setContentSize:CGSizeMake(646, 73)];
self.scrollView.scrollEnabled = YES;
UIImage *bleh = [UIImage imageNamed:#"Digits 2 1"];
UIImageView *raaaa = [[UIImageView alloc]initWithImage:bleh];
[self.scrollView addSubview:raaaa];
}
So the first thing you need to understand is the contentsize property.
The subviews inside the scrollview are its "content". They can be arranged in any way you want inside the view.
In order to make it scroll though, you need to let the scrollview know how much "space" its content is taking up.
In your case, I'm going to guess that your image is 646 x 73 in dimensions. If you set the contentsize to that, the scrollview doesn't need to scroll because it can show all the content at once. However when you increase it, the scrollview now knows there is more content outside of the screen and it will allow you to scroll.
The second thing you need to understand is the position of the subviews. If you set the frame of your imageview to (0,0,646,73), it will be in the "left-most" position inside the scrollview. Thus, it will only scroll to the right (the white space you see).
Try this code and see if you can understand whats happening:
[self.scrollView setContentSize:CGSizeMake(1292, 73)];
UIImage *im = [UIImage imageNamed:#"Digits 2 1"];
UIImageView *v1 = [[UIImageView alloc]initWithImage:im];
UIImageView *v2 = [[UIImageView alloc]initWithImage:im];
v1.frame = CGRectMake(0,0,646,73);
v2.frame = CGRectMake(646,0,646,73);
[self.scrollView addSubView:v1];
[self.scrollView addSubView:v2];

Draw an UIImage on top of a subView and interact with a UIScrollView

I have a ViewController with a UIScrollView and 3 subviews.
I've declared the size of the UIScrollView to have the 3 views by horizontal scrolling.
I'm trying to draw a new image on top of each sub view and when the view will be scrolled I would like that the new image will be scrolled with the subview.
Right now, I managed to draw the new image, but it remains static even when I scroll the views.
x = [button frame].origin.x;
y = [button frame].origin.y;
check = [[UIImageView alloc] initWithFrame:CGRectMake(x+100,y+100, 20, 20)];
check.image = [UIImage imageNamed:#"check.png"];
[self.view addSubview:check];
Don't draw it in your scroll view. Draw it in your custom UIImageView subclass. Or add subviews to your image views.

UIView subview not resizing

I am working on a Universal Application. I have a View-based application which has one view controller. I am adding a UIImageView as a subview.
My problem is that if I put the ImageView in viewDidLoad, it is getting resized in iPad.
But if I add the ImageView to view controller dynamically on button tap event, the UIImageView is not getting resized as per iPad. Instead, it is showing ImageView with 320 x 480 dimensions.
Note: My View Controller has setAutoresizesSubviews to YES.
My code is as below:
-(void)buttonTap
{
UIImage *img = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:#"main-bg" ofType:#"png"]];
UIImageView *imgViewMainBG = [[UIImageView alloc]initWithImage:img];
imgViewMainBG.frame = CGRectMake(0,0,320,480);
imgViewMainBG.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:imgViewMainBG];
[imgViewMainBG release];
[img release];
img=nil;
}
AutoresizingMask is for changing the size of all subviews according to the change in size of the superView whenever the superView's size is changed, it won't resize subviews at the time of adding a subView.
But you can find the bounds or frame of self.view and set the frame property of imageView according to that bounds like below
[imgViewMainBG setFrame:CGRectMake(0, 0, self.view.frame.size.width,
self.view.frame.size.height)];
so that imgViewMainBG is fully visible in ipad
Try changing image view's contentMode property to UIViewContentModeScaleToFill.

initWithFrame with self.view.frame : is it necessary?

I was wondering what was the real meaning of using initWithFrame with this scrollView, because we also set the dimensions of the scrollView after that, and we add the scrollView as a subView of the view.
So why do we need to specify this initWithFrame? I actually don't really understand it when the frame is self.view.frame (I would understand it better if we set a different rectangle, such as 0,0 50,50)
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.contentSize = CGSizeMake(847, 1129);
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
Thanks
self.view in this case is the view containing the scrollview, so the scrollview fills the entire view when set to self.view.frame. Frame and content size are different things - frame of scrollview defines visible part of scrollview, and content size defines the size of scrollable (zoomable) content inside your scrollview.

a problem to load imageView to scrollView

I am an new user of Objective-c. I have a problem to load imageview to scrollview.
I use interface builder to add a scrollview onto view. and then I try to add an imageview on the scrollview by code.
UIImage *image = [[[UIImage alloc]init]autorelease];
image = [UIIamge imageNamed:#"cover.jpg"];
imageView = [[[UIImageView alloc]init]autorelease];
imageVIew.frame = CGRectMake(50.0, 40.0, 200.0,200.0);
imageView.image = image;
[scorllView addSubview:imageView];
then I add another imageView onto the ScrollView with position at (50.0,1000.0)
and length = 200, width = 200 (the screen of ipad is 786*1004)
the photos can appear on the screen. The second photo is not complete, and I try to scroll the screen, however I can't scroll it.
Thanks.
first, you only need
UIImage * image = [UIImage imageNamed:#"cover.jpg"];
class methods(imageNamed: here) that creates the instance of it other than "alloc" would create a autoreleased object.
[UIImage imageNamed:#"cover.jpg"]
is conceptually identical to
[[UIImage alloc] initWithFileContents:#"cover.jpg"] autorelease] // note, alloc once, release once.
(it may not be identical internally, imageNamed: caches the image initWithFileContents: might not)
In order to scroll the scrollView,
there are many things you need to make sure.
One of it is to make sure your scrollView's contentSize is bigger than your scrollView's frame size.
try setting your scrollView.contentSize = CGSizeMake(someBigValue, someBigValue);
The content inside scrollView scrolls inside the scrollView. So your content should be bigger than scrollView's frame size to scroll.
you have to increase contentinset or UIScrollview to make it scrollable,
You can increase bottom,
scorllView.contentInset = UIEdgeInsetsMake(0, 0, 680, 0);
or increase as you need

Resources