2

Swift(iOS) problem: how can I insert UIImage array to subview? I wanted to make a horizontal scroll view in view controller with very little experience with swift. Here is the code:

override func viewDidLoad() {
    super.viewDidLoad()

    configurePageControl()

    var pictures:[UIImage] = [ ]
    pictures.append(UIImage(named: "SOVERMENT1.jpg")!)
    pictures.append(UIImage(named: "SOVERMENT2.jpg")!)
    pictures.append(UIImage(named: "SOVERMENT3.jpg")!)
    pictures.append(UIImage(named: "SOVERMENT4.jpg")!)



    scrollView.delegate = self
    self.view.addSubview(scrollView)
    for index in 0..<4 {

        frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
        frame.size = self.scrollView.frame.size
        self.scrollView.pagingEnabled = true

        var subView = UIView(frame: frame)
        **subView.backgroundColor = [UIColor colorWithPatternImage : ]**
        self.scrollView.addSubview(subView)
    }

This is first problem. How can I insert pictures into subview.backgroundColor? I searched it for 1~2hours, but I couldn't find any answer.

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 4, self.scrollView.frame.size.height)
    pageControl.addTarget(self, action: Selector("changePage:"), forControlEvents: UIControlEvents.ValueChanged)


}


func configurePageControl() {


   ** self.pageControl.numberOfPages = pictures.count **
    self.pageControl.currentPage = 0

And here. Why array pictures cannot be counted?

3 Answers 3

5

Unless these are repeatable pattern images, I think you want to use a UIImageView. I also don't think you need to use a UIPageControl, because you're only using one UIViewController. You can say something like:

    override func viewDidLoad() {
        super.viewDidLoad()


        var pictures:[UIImage] = [ ]
        pictures.append(UIImage(named: "SOVERMENT1.jpg")!)
        pictures.append(UIImage(named: "SOVERMENT2.jpg")!)
        pictures.append(UIImage(named: "SOVERMENT3.jpg")!)
        pictures.append(UIImage(named: "SOVERMENT4.jpg")!)



        self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * CGFloat(pictures.count), scrollView.frame.size.height)
        for index in 0..<pictures.count {
            var frame = CGRectZero
            frame.origin.x = scrollView.frame.size.width * CGFloat(index)
            frame.size = scrollView.frame.size
            scrollView.pagingEnabled = true

            var subView = UIImageView(frame: frame)
            subView.image = pictures[index]
            subView.contentMode = .ScaleAspectFit
            self.scrollView.addSubview(subView)
        }



    }
Sign up to request clarification or add additional context in comments.

Comments

0

You better use UICollectionView for such purposes

Comments

0

For setting the backgroundColor call the UIColor initializer:

for picture in pictures {
    subView.backgroundColor = UIColor(patternImage: picture)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.