Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get real size UIView with autolayout

I'm a new developer iOS.

I use auto-layout.

I have a problem. I can not get real size of UIView after constraints.

Example, when I want get size of UIView with constraint

self.container.frame.size.height 

It alway return 550 for all device size. But I can see it have difference size when I run it on difference device.

Thanks

like image 452
Chicken Avatar asked Dec 26 '14 05:12

Chicken


People also ask

What is Autolayout aspect ratio?

If you select Aspect Ratio for multiple items, Auto Layout chooses the width of one of the items for the numerator and the height of another item for the denominator. To change the initial aspect ratio, edit the Multiplier field of the Attributes inspector for the constraint.

How do I use Autolayout constraints?

Auto Layout dynamically calculates the size and position of all the views in your view hierarchy, based on constraints placed on those views. For example, you can constrain a button so that it is horizontally centered with an Image view and so that the button's top edge always remains 8 points below the image's bottom.

Does UIImageView have intrinsic content size?

Here is the essential problem: the only way in which UIImageView interacts with Auto Layout is via its intrinsicContentSize property. That property provides the intrinsic size of the image itself, and nothing more.

How do I use Autolayout in Xcode?

To create constraints select the button and click the Align icon in the auto layout menu. A popover menu will appear, check both “Horizontal in container” and “Vertically in container” options to center the button on the screen. Then click the “Add 2 Constraints” button. Run the application.


2 Answers

Before your "getting real size code", insert the following code:

[view setNeedsLayout]; [view layoutIfNeeded]; 
like image 71
Allen Avatar answered Sep 23 '22 15:09

Allen


Since everybody assumes the access is done via a UIViewController and not inside a UIView subclass, the answers don't prove really useful. The correct way to get the correct size of a UIView subclass is in layoutSubviews.

like image 21
Leandros Avatar answered Sep 21 '22 15:09

Leandros