1

Previously, i thought UIImageView's image cannot extend beyond the view. However, recently i read the book Programming in IOS8, it says:

You should also pay attention to a UIImageView’s clipsToBounds property; if it is false, its image, even if it is larger than the image view and even if it is not scaled down by the contentMode, may be displayed in its entirety, extending beyond the image view itself.

And then i did some simple test, that's true.

Here is the question: What's the purpose for Apple to design UIImageView with this feature? I, for now, cannot find the advantage of it. I think this makes contentMode a fake property.

Here is an example:

It is a refreshView with customized pull-to-refresh animation above in a UITableView. I set up a UIImageView with the same size as the refreshView and add it as a background view. Relevant code here:

let imgView = UIImageView()
imgView.frame = self.bounds
imgView.image = UIImage(named: "refresh-view-bg.png")
imgView.contentMode = .ScaleAspectFill
imgView.clipsToBounds = true
self.addSubview(imgView)

It works fine:

enter image description here

However, when i annotate this line:

imgView.clipsToBounds = true

It turns like that: enter image description here

The image extend beyond the UIImageView

ps: I'm not using autoLayout in this demo

duan
  • 8,515
  • 3
  • 48
  • 70
  • 2
    It wasn't done specifically for `UIImageView` but is true for any `UIView`. – A-Live Mar 02 '15 at 10:29
  • But image is a property not a subView @A-Live – duan Mar 02 '15 at 11:04
  • 1
    It doesn't matter, being an instance of `UIView` an `UIImageView` can draw content outside of its bounds like any other `UIView`, it is also not directly related to auto layout but is a consequence of the drawing implementation. – A-Live Mar 02 '15 at 11:27

1 Answers1

1

This is due to autolayout. UIImageView returns its -intrinsicContentSize by the calculation of image size. If you want to avoid that you should set a width (or height) constraints, or play with content hugging/compression resistance priorities if you want to share or not the space when you view is laid out with other views around.

Community
  • 1
  • 1
Andrea
  • 26,120
  • 10
  • 85
  • 131
  • But when i don't give it layout constraint, just set up UIImageView's frame. This problem occurs too. – duan Mar 02 '15 at 10:50
  • Because if your project has autolayout enabled, autolayouf will work on all views and doesn't care about your frames if it founds something that could be in conflict. Stop using frames, use autolayout the better the sooner. when you place views even if you do not set constraints autolayout converts autoresizing masks into constraint. – Andrea Mar 02 '15 at 11:23
  • I think when you turn the project's Autolayout enabled, if you don't give a view constraint, the system will use its frame data. By the way, i do like AutoLayout. but basically, i assume the problem is not relevant to layout :) @Andrea – duan Mar 04 '15 at 03:27
  • @Carrl I'm sorry but it's not, that is why you have a property called setTranslatesAutoresizingMaskIntoConstraints, you can check the first WWDC video about autolayout, they explain clearly. Probably is not the problem, but if was you I would look into that anyway and also try to use the new feauture of xcode 6 to debug views, in this way you will perfectly see frames in 3d way – Andrea Mar 04 '15 at 09:19