How to present side by side UIview using UIScrollView.
Follow this steps and get paging view side by side
STEP 1:- Create New Project "PagingViewDemo"
STEP 2:- Go main.Storybord and Drag and drop UIScrollView.
STEP 3:- and Drag and drop UIView on UIScrollView.
STEP 4:- and Drag and drop UIImageView on UIView
STEP 4:- set The consternt.
STEP 5:- Copy and Pest this code.
//Mark:- My UIView Class
class myViewClass: UIView
{
@IBOutlet var myImage: UIImageView!
}
//MARK:- my UIViewController class
import UIKit
class PagingViewDemo: UIViewController,UIScrollViewDelegate
{
// MARK:- ---------- OUTLET Declaration ------------
@IBOutlet var scrollingPageControl: UIPageControl!
@IBOutlet var pagingScrollView: UIScrollView!
// MARK:- ---------- Variable Declaration ------------
var imageArray = [String]()
// MARK:- ---------- VIEW LifeCycel ------------
override func viewDidLoad() {
super.viewDidLoad()
imageArray = ["Banner1.jpg","Banner2.png","Banner3.jpg","Banner4.jpg"]
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
LoadBanerImage()
}
// MARK:- ---------- function Declaration ------------
func LoadBanerImage()
{
pagingScrollView.isPagingEnabled = true
pagingScrollView.contentSize = CGSize(width: self.view.bounds.width * CGFloat(imageArray.count), height: 0)
pagingScrollView.showsHorizontalScrollIndicator = false
pagingScrollView.delegate = self
scrollingPageControl.numberOfPages = imageArray.count
var index = 0
while index < imageArray.count
{
if let myView = Bundle.main.loadNibNamed ("myViewClass", owner: self, options: nil)?.first as? myViewClass
{
myView.myImage.image = UIImage(named: imageArray[index])
pagingScrollView.addSubview(myView)
myView.frame.size.width = self.view.bounds.size.width
myView.frame.origin.x = CGFloat(index) * self.view.bounds.size.width
}
index += 1
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let page = scrollView.contentOffset.x / scrollView.frame.size.width
scrollingPageControl.currentPage = Int(page)
}
}
I Hop you Enjoy this tutorial keep touch in this blog for more update
STEP 1:- Create New Project "PagingViewDemo"
STEP 2:- Go main.Storybord and Drag and drop UIScrollView.
STEP 3:- and Drag and drop UIView on UIScrollView.
STEP 4:- and Drag and drop UIImageView on UIView
STEP 4:- set The consternt.
STEP 5:- Copy and Pest this code.
//Mark:- My UIView Class
class myViewClass: UIView
{
@IBOutlet var myImage: UIImageView!
}
//MARK:- my UIViewController class
import UIKit
class PagingViewDemo: UIViewController,UIScrollViewDelegate
{
// MARK:- ---------- OUTLET Declaration ------------
@IBOutlet var scrollingPageControl: UIPageControl!
@IBOutlet var pagingScrollView: UIScrollView!
// MARK:- ---------- Variable Declaration ------------
var imageArray = [String]()
// MARK:- ---------- VIEW LifeCycel ------------
override func viewDidLoad() {
super.viewDidLoad()
imageArray = ["Banner1.jpg","Banner2.png","Banner3.jpg","Banner4.jpg"]
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
LoadBanerImage()
}
// MARK:- ---------- function Declaration ------------
func LoadBanerImage()
{
pagingScrollView.isPagingEnabled = true
pagingScrollView.contentSize = CGSize(width: self.view.bounds.width * CGFloat(imageArray.count), height: 0)
pagingScrollView.showsHorizontalScrollIndicator = false
pagingScrollView.delegate = self
scrollingPageControl.numberOfPages = imageArray.count
var index = 0
while index < imageArray.count
{
if let myView = Bundle.main.loadNibNamed ("myViewClass", owner: self, options: nil)?.first as? myViewClass
{
myView.myImage.image = UIImage(named: imageArray[index])
pagingScrollView.addSubview(myView)
myView.frame.size.width = self.view.bounds.size.width
myView.frame.origin.x = CGFloat(index) * self.view.bounds.size.width
}
index += 1
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let page = scrollView.contentOffset.x / scrollView.frame.size.width
scrollingPageControl.currentPage = Int(page)
}
}
I Hop you Enjoy this tutorial keep touch in this blog for more update
Comments
Post a Comment