Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UIImageView size after autolayout is applied?

My storyboard has UIImageView which is automatically positioned and scaled by autolayout constraints.

When application runs, this UIImageView is properly resized and positioned according to device screen size and autolayout constraints.

How I can get UIImageView frame after it is displayed on the screen? Common UIImageView.bounds and UIImageView.frame values return original values which were used in storyboard and does not reflect new UIImageView size.

like image 758
Kibernetik Avatar asked Feb 11 '15 19:02

Kibernetik


People also ask

Does Uiimageview have intrinsic content size?

A vanilla UIView instance, for example, does not have an intrinsic content size. This means that you need to add four explicit constraints to describe the size and position of a UIView instance in a user interface.

What is multiplier in AutoLayout?

Multiplier is there for creating Proportional Constraint. Auto Layout calculates the first item's attribute to be the product of the second item's attribute and this multiplier . Any value other than 1 creates a proportional constraint. replace : with / - you will understand what it means.

What is the use of AutoLayout?

Auto layout is a property you can add to frames and components. It lets you create designs that grow to fill or shrink to fit, and reflow as their contents change. This is great when you need to add new layers, accommodate longer text strings, or maintain alignment as your designs evolve.

How do I create a constraint in AutoLayout?

There are three main options for setting up Auto Layout constraints in Interface Builder: You can control-drag between views, you can use the Pin and Align tools, and you can let Interface Builder set up the constraints for you and then edit or modify the results.


2 Answers

In order to get the right frame/bounds of your UIImageView after resizing, you need first ask auto-layout to update that layout using [yourImageView layoutIfNeeded]. that will solve your constraints and update your yourImage.bounds.

[myImageView layoutIfNeeded];
NSLog(@"w: %f, h: %f", myImageView.bounds.size.width, myImageView.bounds.size.height);
like image 130
tbaranes Avatar answered Oct 04 '22 00:10

tbaranes


Check these methods:

- (void)viewWillAppear:(BOOL)animated {
// view is about to be added to hieararchy
}

- (void)viewDidAppear:(BOOL)animated {
// view was added
}

- (void)viewDidLayoutSubviews {
// VC just laid off its views
}
like image 36
orkenstein Avatar answered Oct 03 '22 22:10

orkenstein