---
title: "How to set scrollView images in centered in ios?"  
description: "How to set scrollView images in centered in ios?"  
author: "Barbara Jones"  
published: 2014-10-28  
updated: 2014-10-28  
canonical: https://www.mindstick.com/forum/2443/how-to-set-scrollview-images-in-centered-in-ios  
category: "iphone"  
tags: ["iphone", "ios 7"]  
reading_time: 2 minutes  

---

# How to set scrollView images in centered in ios?

i'm [trying](https://answers.mindstick.com/qa/93698/6-mistakes-couples-are-trying-to-save-money) to create a scrollView with a UIPageControl. This [works fine](https://answers.mindstick.com/qa/115732/my-software-works-fine-on-windows-10-but-crashes-on-windows-11-why), but the [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is the [images](https://www.mindstick.com/interview/1067/what-is-the-php-predefined-variable-that-tells-the-what-type-of-images-that-php-support) wont center in each [page](https://www.mindstick.com/articles/13031/why-to-make-a-wikipedia-page) of the scrollView. it is aligned to the right. How can i center the images always?\

## viewDidLoad

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

\

**[rest](https://www.mindstick.com/forum/12745/how-do-i-use-webapi-rest-correctly-when-other-params-are-needed) of the [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best)**

```
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()}
```

## Replies

### Reply by Lillian Martin

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.


---

Original Source: https://www.mindstick.com/forum/2443/how-to-set-scrollview-images-in-centered-in-ios

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
