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
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.
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.
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.
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.
Before your "getting real size code", insert the following code:
[view setNeedsLayout]; [view layoutIfNeeded];
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With