I have been playing around with iPhone interface building using only code and not using IB.
Now I'm facing the following problem:
How can I set an image to have a width based on the main view it is located on and to let it have a margin of for example 50 pixels on both sides. ( it should also work with rotation so the width should be flexible ).
I have tried setting the size with frame.size.width - 50 for example but this doesn't work when the screen rotates. Another thing I tried is using the autoresizing masks but I'm not completely understanding how this works.
Does one still need to set a frame for an image or is this completely overruled by the autoresizing masks? And if it's overruled how can I give the image a fixed height and a flexible width?
The book I'm using ( advanced iOS4 programming ) is not very clear in this matter and most examples I find on the net are for interface builder.
Thanks for your time.
Kind regards, Jasper
An integer bit mask that determines how the receiver resizes itself when its superview's bounds change.
Autoresizing masks is a layout method using a bit mask, or an encoded set of flags, that defines how a view should respond to changes in the superview's bounds. The properties available in a autoresizing mask on a view are: Flexible Top Margin, meaning resizing can change the view's top margin.
Horizontally:
UIViewAutoresizingFlexibleWidth
UIViewAutoresizingFlexibleRightMargin
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin
UIViewAutoresizingFlexibleLeftMargin
Vertically:
UIViewAutoresizingFlexibleHeight
UIViewAutoresizingFlexibleBottomMargin
UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin
UIViewAutoresizingFlexibleTopMargin
Combine one from the first section with one from the second with a |
. For example, to have it resize horizontally and keep the same size and distance from the top vertically, you'd use UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin
.
You can use other combinations to do some more tricky things, but those are the basics.
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