I have an UIImage in my app, and I had to uncheck the auto layout box and set autosizing with the options shown in the image below so the image will show properly in both screen sizes (3.5 inch, and 4 inch)
My question is, how can I set these options in code?
I tried this but it doesn't seem to work? Am I doing something wrong?
file.m
// Autosizing ImageOne
ImageOne.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleTopMargin;
The presence of the I-beam on the left, right, and bottom means that they're fixed (i.e. not flexible), so you should not use
UIViewAutoresizingFlexibleLeftMargin
,UIViewAutoresizingFlexibleRightMargin
, orUIViewAutoresizingFlexibleBottomMargin
. The absence of the top I-beam means that you should employUIViewAutoresizingFlexibleTopMargin
.The presence of the width arrow means that the width is flexible, and that you should therefore include
UIViewAutoresizingFlexibleWidth
. But the absence of the height arrow means thatUIViewAutoresizingFlexibleHeight
should not be used.Thus, that yields:
By the way, you can verify this empirically. Temporarily add a view in Interface Builder using the combinations of springs and struts that you shared with us:
Then you can use a routine like the following to examine the auto resizing mask of that view:
The above routine will confirm you that you want
UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth
.You can also confirm this in the debugger, as it provides a good tool to examine the configuration of views (their frames and autosizing masks). Run the app in the debugger, and once the view has been presented, hit the pause button. Then, at the
(lldb)
prompt, type:That will generate output like so:
As you can see, this is also reporting that the width and top margin autosizing masks have been used (where it says
W+TM
). This output can also be useful for diagnosing the frame size.By the way, in addition to setting the auto sizing bit mask as discussed, you may want to confirm the content mode and the clipping setting. Notably, you may also want to make sure you clip the image view to its bounds (or else the image may overflow the bounds, misleading you on the actual size of the frame):
If you want to take the image full screen then make your setting like this:
Hope this helps.. :)
Set the
UIImageView
contentMode
property to UIViewContentModeScaleAspectFit