ios - UIImageViews not scaling correctly in UIScrollView with paging -
i'm trying build simple uiscrollview
several images paging enabled.
however, can't seen images correctly resized bounds of uiscrollview
. images bigger bounds, messing paging.
i have uiscollview
, uipagecontrol
in interface builder
, link in viewcontroller swift file.
here viewdidload
method (pageimages
defined var pageimages: [uiimage] = []
, pageviews
var pageviews: [uiimageview?] = []
:
override func viewdidload() { scrollview.delegate = self scrollview.pagingenabled = true scrollview.scrollenabled = true scrollview.showshorizontalscrollindicator = false scrollview.showsverticalscrollindicator = false scrollview.bounces = true scrollview.scrollstotop = false pageimages = [uiimage(named: "image1")!, uiimage(named: "image2")!, uiimage(named: "image3")!, uiimage(named: "image4")!, uiimage(named: "image5")!] let pagecount = pageimages.count pagecontrol.currentpage = 0 pagecontrol.numberofpages = pagecount (var i=0; i<pagecount; i++) { var xorigin: cgfloat = cgfloat(i) * scrollview.bounds.size.width let imageview: uiimageview = uiimageview(frame: cgrectmake(xorigin, 0, scrollview.bounds.size.width, scrollview.bounds.size.height)) imageview.image = pageimages[i] imageview.contentmode = uiviewcontentmode.scaleaspectfill imageview.clipstobounds = true scrollview.addsubview(imageview) } let pagesscrollviewsize = scrollview.frame.size scrollview.contentsize = cgsize(width: pagesscrollviewsize.width * cgfloat(pageimages.count), height: pagesscrollviewsize.height) }
my scrollviewdidscroll
method follows:
func scrollviewdidscroll(scrollview: uiscrollview) { let pagewidth = self.scrollview.frame.size.width let page = int(floor((self.scrollview.contentoffset.x - pagewidth/2)/pagewidth) + 1) self.pagecontrol.currentpage = page }
can spot mistake? have set contentmode
maybe in viewwillappear
or viewdidappear
?
try setting imageviews sizes under:
- (void)viewdidlayoutsubviews
at point, autolayout had finished resizing views/subviews according layout system defined on interface builder if calculations correct (according requirements) should work out fine.
Comments
Post a Comment