Home > DeveloperSection > Forums > How to set scrollView images in centered in ios?
Barbara Jones

Total Post:13

Points:91
Posted on    October-28-2014 2:15 AM

 iPhone iPhone  Ios 7 
Ratings:


 1 Reply(s)
 680  View(s)
Rate this:
i'm trying to create a scrollView with a UIPageControl. This works fine, but the problem is the images wont center in each page of the scrollView. it is aligned to the right. How can i center the images always?

viewDidLoad

self.pageControl = UIPageControl()
self.pageControl?.frame = CGRectZero
self.pageControl?.currentPage = 0
self.pageControl?.numberOfPages = self.imageArray!.count
self.pageControl?.tintColor = UIColor.whiteColor()
self.pageControl?.userInteractionEnabled = false
self.navigationItem.titleView = self.pageControl?
for var i = 0; i<self.imageArray?.count; i++ {
    self.pageViews.append(nil)
}
let pagesScrollViewSize = self.scrollView.frame.size
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * CGFloat(self.imageArray!.count), pagesScrollViewSize.height)
self.loadVisiblePages()

rest of the methods

func loadPage(page: Int) {
if page < 0 || page >= self.imageArray!.count {
        return
    }
    if let pageView = pageViews[page] {
    } else {
        let newPageView = UIImageView(image: self.imageArray![page] as UIImage)
        var frame = scrollView.bounds
        frame.origin.x = (frame.size.width * CGFloat(page))
        frame.origin.y = 0.0
        newPageView.contentMode = .ScaleAspectFit
        newPageView.frame = frame
        scrollView.addSubview(newPageView)
        newPageView.backgroundColor = UIColor.blackColor()
        pageViews[page] = newPageView
    }
}
func purgePage(page: Int) {
    if page < 0 || page >= self.imageArray!.count {
        return
    }
array
    if let pageView = pageViews[page] {
        pageView.removeFromSuperview()
        pageViews[page] = nil
    }
}
func loadVisiblePages() {
    let pageWidth = scrollView.frame.size.width
    let page = Int(floor((scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth * 2.0)))
    pageControl?.currentPage = page
    let firstPage = page - 1
    let lastPage = page + 1
    for var index = 0; index < firstPage; ++index {
        purgePage(index)
    }
    for var index = firstPage; index <= lastPage; ++index {
        loadPage(index)
    }
    for var index = lastPage+1; index < self.imageArray!.count; ++index {
        purgePage(index)
    }
}
func scrollViewDidScroll(scrollView: UIScrollView!) {
    loadVisiblePages()
}


Lillian Martin

Total Post:27

Points:189
Posted on    October-28-2014 2:16 AM

I found here you use let to declare a constant UIImageView, I think you should use var.

let newPageView = UIImageView(image: self.imageArray![page] as UIImage)
Actually, I can find any problem besides this, I think the reason you can't set your image to the center because that all the operation after this declaration didn't work, so you can't set the right frame, the right contentMode to the UIImageView.

Don't want to miss updates? Please click the below button!

Follow MindStick