Reputation: 3607
I am facing a strange issue while resizing images in IB (Xcode 5). First, I drag & drop a UIImageView
on my viewController
in a storyBoard. I then resize and set an image for it, which works properly. However, once I try to resize the UIImageView
control after setting the image, it starts behaving strangely. Sometimes it occupies the complete viewController
, other times the image becomes so big that it is out of bounds.
I tried to set a smaller image, but it resulted in more strange behavior. Now if I try to resize it, sometimes it can't be resized by dragging. Sometimes the UIImageView
is resizes properly but the contained image remains the same size. Any suggestions?
This was not happening when using Xcode 4.
Upvotes: 0
Views: 897
Reputation: 536027
There are two issues here.
For an image to be resized to the same size as its image view, it is essential that its content mode be set to UIViewContentModeScaleToFill
, UIViewContentModeScaleAspectFit
, or UIViewContentModeScaleAspectFill
. You can make this setting in the attributes inspector.
NOTE: If you do not do this, then the image will be shown at its own natural size. If the image is huge, it will be shown huge. Moreover, it will be shown huge even if the image view is small, because the image view does not clip its image. If you want to the image view to clip its image (so that you can see where the bounds of the image view really are), set the image view's Clips To Bounds (
clipsToBounds
). Again, you can make this setting in the attributes inspector.
In Xcode 5, you are using auto layout by default. This means that the image view has the size of its image by default. If you don't want that, you must use constraints to set the height and width of the image view. You will then probably also have to use constraints to set its position. Do NOT run the app until all auto layout warnings in Interface Builder have been taken care of, or you will get a big surprise at runtime.
Upvotes: 2