ios - How to keep an Aspect-fixed UIView filling up the screen in any screen orientation programmatically in swift? -
i want simple effect:
that have picture automatically orients according device/screen orientation, alway fill whole screen without stretching aspect doesn't change.
i work xcode though got lot of answer storyboard , ui builder,i don't such ways work have no idea how works, trying find way programmatically instead.
i've tried set image view subview, doesn't fix after device rotated, either stretch fill screen or move somewhere else size unchanged (when trie recalculate frame , apply subview), tried override drawrect func, of uiview, frame works fine animation of rotating broken (it stretches self during rotation , skip final frame).
the perfect effect want ios original photo app, when check photo, resizes fix screen, , when rotate screen, animation works perfect, rotating , scaling happens in same time , no skip of stretch happens. please give me help?
thanks!!!!!
the way set frame:
func locating (imgsize:cgsize,scrsize:cgsize) ->cgrect { var size:cgsize var ori:cgpoint if ((imgsize.height / imgsize.width) * scrsize.width>=scrsize.height){ size = cgsize(width:scrsize.width,height:imgsize.height*(scrsize.width/imgsize.width)) ori = cgpoint(x:0,y:scrsize.height-imgsize.height*(scrsize.width/imgsize.width)) } else { size = cgsize(width:imgsize.width*(scrsize.height/imgsize.height),height:scrsize.height) ori = cgpoint(x:-(imgsize.width*(scrsize.height/imgsize.height)-scrsize.width)/2,y:0) } return cgrect(origin:ori,size:size)
}
apart math's stuff! here solutions can work you!
- set image width , height superview width , height.
self.imageview.frame = self.imageview.superview?.bounds
then set
imageview's
contentmode
propertyaspectfill
oraspectfit
self.imageview.contentmode = uiviewcontentmodescaleaspectfit;
self.imageview.contentmode = uiviewcontentmodescaleaspectfill;
Comments
Post a Comment