ios - UIImage Scale Aspect Fill and Clip Subviews not working in UICollectionView -


i have uicollectionview cell containing single uiimageview. i'm setting uiimageview frame.size match cell's frame.size , explicitly asking uiimageview scale aspect fill & clip subview following way in cellforitematindexpath method:

let cell:uicollectionviewcell = collectionview.dequeuereusablecellwithreuseidentifier("cell", forindexpath: indexpath) as! uicollectionviewcell  ...   var imageview = uiimageview(image: self.eventimages[indexpath.row]) imageview.frame.size = cell.frame.size cell.contentmode = uiviewcontentmode.scaleaspectfill cell.clipstobounds = true imageview.contentmode = uiviewcontentmode.scaleaspectfill imageview.clipstobounds = true cell.addsubview(imageview) 

the result stretched out image (image a), when click (tap) image "magically" resolves aspect fill & clip subviews (image b) . can't seem understand why happening after tap , not when loading images first time. fetched asynchronously server, scaling occurs after tap of image (there no tap functionality implemented).

note: uicollectionview set in storyboard view controller, both collection view , reusable cell have aspect fill , clip subviews enabled.

image (wrong scaling - stretched out , no cropping):

img a

image b (correct scaling after tap):

img b

it simple this.

imageview.contentmode = uiviewcontentmode.scaleaspectfill; imageview.layer.masktobounds=yes; 

Comments