Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Scroll View with a page control using swift? [closed]

I am trying to create a Page View controller with numbers of view. I want a simple code sample that will use UIPageViewController.

like image 578
Anish Parajuli 웃 Avatar asked Mar 16 '15 10:03

Anish Parajuli 웃


People also ask

How do I use page control in Swift?

Enter Swift as Language and choose Next. For this tutorial some images are needed, so download the movies posters and drag them to the Assets Library. Go to the Storyboard and drag a Scroll View from the Object Library to the main view. Next, drag a Page Control from the Object Library below the scroll View.

How do I make my stack view scrollable?

Add a UIStackView to the UIScrollView. Set the constraints: Leading , Trailing , Top & Bottom should be equal to the ones from UIScrollView. Set up an equal Width constraint between the UIStackView and UIScrollView . Set Axis = Vertical, Alignment = Fill, Distribution = Equal Spacing, and Spacing = 0 on the UIStackView.


1 Answers

import UIKit  class DummyVC: UIViewController, UIScrollViewDelegate {      let scrollView = UIScrollView(frame: CGRect(x:0, y:0, width:320,height: 300))     var colors:[UIColor] = [UIColor.red, UIColor.blue, UIColor.green, UIColor.yellow]     var frame: CGRect = CGRect(x:0, y:0, width:0, height:0)     var pageControl : UIPageControl = UIPageControl(frame: CGRect(x:50,y: 300, width:200, height:50))          override func viewDidLoad() {         super.viewDidLoad()          configurePageControl()          scrollView.delegate = self         scrollView.isPagingEnabled = true          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              let subView = UIView(frame: frame)             subView.backgroundColor = colors[index]             self.scrollView .addSubview(subView)         }          self.scrollView.contentSize = CGSize(width:self.scrollView.frame.size.width * 4,height: self.scrollView.frame.size.height)         pageControl.addTarget(self, action: #selector(self.changePage(sender:)), for: UIControlEvents.valueChanged)      }      func configurePageControl() {         // The total number of pages that are available is based on how many available colors we have.         self.pageControl.numberOfPages = colors.count         self.pageControl.currentPage = 0         self.pageControl.tintColor = UIColor.red         self.pageControl.pageIndicatorTintColor = UIColor.black         self.pageControl.currentPageIndicatorTintColor = UIColor.green         self.view.addSubview(pageControl)      }      // MARK : TO CHANGE WHILE CLICKING ON PAGE CONTROL     func changePage(sender: AnyObject) -> () {         let x = CGFloat(pageControl.currentPage) * scrollView.frame.size.width         scrollView.setContentOffset(CGPoint(x:x, y:0), animated: true)     }      func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {          let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)         pageControl.currentPage = Int(pageNumber)     }  } 
like image 58
Anish Parajuli 웃 Avatar answered Sep 28 '22 01:09

Anish Parajuli 웃