Changing UIView (UIImageView specifically) frame programmatically not working - ios

I load a UIImageView with a certain height and width. after I initialise it, I would like to change it's height programmatically. I am using the following code to do so, but it is not working.
Note: The UIImageView has constraints set on it.
CGRect newframe = imageView.frame;
newframe.size.height= 500;
[imageView setFrame:newframe];
Calling setFrame is not changing anything on the image. What am I doing wrong?
Thanks

Modify the height constraint after initializing your UIImageView instead. See the second answer here for an example: Change frame programmatically with auto layout

Related

Change size of views inside ContainerView programmatically

I have a container view which is equally divided between two UIViews like this:
The portion in black is my UIView 1, which I am currently not using. My UIView 2 contains a UISegmented Bar and a UITableView.
The hierarchy of my views look something like this:
Now, my requirement is I want to resize my view2 to cover entire container view dynamically and view1 to go away based on some condition. Currently I am not worried about that condition, I just tried to resize my view2 using the following code inside - (void)viewDidLoad
CGRect newFrame = self.view2.frame;
newFrame.size.width = 200;
newFrame.size.height = 200;
newFrame.origin.x=0;
newFrame.origin.y=0;
[self.view2 setFrame:newFrame];
Here view2 is the outlet to my view2 in interface builder.
But, nothing is changing from the above code. I tried to find any other way but had no help. So please help me find out my mistake in my current technique or tell me how some other technique to do it.
Thanks in advance!
The code to change frame seems fine, just remove the view from superview, set the frame and add to superview again programmatically.
[self.view2 removeFromSuperview];
// set the frame
CGRect newFrame = self.view2.frame;
newFrame.size.width = 200;
newFrame.size.height = 200;
newFrame.origin.x=0;
newFrame.origin.y=0;
[self.view2 setFrame:newFrame];
//add self.view2 again wherever it was
[myView addSubView:self.view2];

Programmatically set size of UIView from Storyboard

I have placed UIView in UIScrollView in Storyboard. The UIView is pinned to the all the sides of the UIScrollView. I am trying to set the size of UIView programatically because it is needed for scrolling to work properly. How can I programmatically set the width and height of the UIView?
Thanks for any help as I am trying to solve the problem already for a week...
You'll need to change the frame of your view as below:
yourView.frame = CGRectMake(x,y,calculatedWidth,calculatedHeight);
Plus, you'll also need to check for the scroll view's frame so that it enlarges as per your view's width and height.
You need to set new frame to change width, height, x or y for example:
view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, newWidth, newHeight);

iOS View is not expanding the way I want it

I have a UIViewController with a UICollectionView and a UIView at the bottom. The way I put it together is displayed in the image below
The yellow square is the UICollectionView and the red is the UIView. This works out just fine. But now I want to resize the UIView because it sometimes contains more info and needs to be bigger. So I tried this:
[self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height + 10)];
But this expands the UIView at the bottom and it is not visible. I guess this is because I do not have the correct constraints? I also tried to subtract the origin.y with the same amount and this works only the UICollectionView doesn't get resized with the new height. So how do I tackle this problem?
If you are using autolayout, you should not be setting the frame from your code. Instead you should modify the constant of a constraint that is causing your view to be the incorrect size. You can have an IBOutlet to that constraint and you can change it's constant property. Then call setNeedsLayout on your view controller's view
When setting constraints on your storyboard or in a xib file, animations perform animations on the constraints instead of the sizes and positions.
First create a outlet reference of the constraint which will change (in your case the top space of your UIView to the top layout guide) in the header file of your view controller.
When you want to animate a view, you now have to update its constraints and ask to layout the views.
For example :
[UIView animateWithDuration:0.2
animations:^{
viewYConstraint.constant -= 44;
[self.view layoutIfNeeded];
}
]
//Now don't forget to update constraints
[self.view updateConstraints];

Autolayout UIImageView with programatic re-size not following constraints

Sorry for the bother but I'm trying to lazy load several imageViews and then resize it proportionately to the content in a UITableView. I'm also trying (Unwisely perhaps?) to use Autolayout basically for the first time. And I'm not understanding why the constraints aren't working in this case. Here's the code that I'm using to resize the UIImageView after I've loaded the proper image into it.
// Scale the image view and return it.
- (UIImageView *) scaleImageViewForScreenWidth:(UIImageView *)imageView {
UIImage *imgFromView = [imageView image];
CGRect newFrame = CGRectMake(0, 0, imgFromView.size.width, imgFromView.size.height);
float imgFactor = newFrame.size.height / newFrame.size.width;
newFrame.size.width = [[UIScreen mainScreen] bounds].size.width;
newFrame.size.height = newFrame.size.width * imgFactor;
[imageView setFrame:newFrame];
return imageView;
}
As far as constraints are concerned. I'm trying to attach a Label and UIImageView with a shadow to the bottom of the main imageview. Here are the constraints that I'm applying to a bottom shadow in the imageview. The bottom shadow constraints are:
Height Equals: 83
Align Bottom to: Background Image View
LeadingSpace to: Table View Cell
I'm not getting what I want though. Any ideas? I feel like I'm fighting autolayout.
As requested! Here's some guidelines for using Autolayout that should help a lot.
The first thing is that the ideal approach is for you to set things up in Interface Builder so that no further programming is required. So, if - for example - the bounds of your view change, then your view will adjust itself automatically as required.
If that doesn't do the business, then you may have to update the constraints programmatically. Now, the golden rules as I mentioned is that you update the constraints. Resist the temptation to update the underlying UIView frame!
So, you'll do something like:
_myWidthConstraint.constant = 300.f;
The next thing to note is that you should do this in a specific place, and that is in your UIView subclass method updateConstraints:
- (void)updateConstraints
{
[super updateConstraints];
_myWidthConstraint.constant = 300.f;
}
How do you trigger that? By invoking:
[self setNeedsUpdateConstraints];
Hope this helps! For further info check Ole Begemann's excellent article 10 Things You Need To Know About Cocoa Autolayout.
Don't forget the WWDC videos. These are essential!
Also, now there's a book iOS Auto Layout Demystified . Although I've bought it, I haven't had a chance to read it yet. It does look pretty good though.
I was also facing the same issue. the image inside the imageview was too big for the imageview and the contentMode was set to AspectFill.
I solved this by unchecking 'Autoresize Subviews".

Image resizing in Scrollview

I have an ImageView in a ScrollView and resizing the ScrollView at some point. The problem is, that the ImageView is resizing to the same size as the ScrollView, but I want it to keep the original image size.
What I'm doing is this:
scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); // fullscreen scrollview
[scrollView setContentMode:UIViewContentModeTopLeft]; //i want the content to be on the top left, thats correct right?
[scrollView setContentSize:CGSizeMake(imageInScrollView.frame.size.width, imageInScrollView.frame.size.height)]; // now im setting the size of the content, which is like 200x200 but it shows in fullscreen
Please read the comments next to my code, thank you!
You need to change the autoresizingMask for the UIView
autoresizingMask
An integer bit mask that determines how the receiver resizes itself
when its superview’s bounds change.
You can do this like this:
[myImageView setAutoresizingMask:UIViewAutoresizingNone];
The contentSize of UIScrollView does not affect drawing, it is only for scrolling purpose (ie defines a rect within which the user may scroll).
Please give more info about imageInScrollView and the UIImageView it is contained in. In particular check its frame property is correct to what you want and check its autoresizingMask : you probably want something like
imageView.autoresizingMask = UIViewAutoresizingNone;
Lastly, you should do :
[scrollView setContentSize:imageView.frame.size];
as this is more simple

Resources