EricDXS
EricDXS

Reputation: 882

Swift: Scrollview with 2 pages

I have been struggling a bit with this: I need a UIScrollView with 2 pages(horizontal, paging enabled). When the viewcontroller loads i need to show some information(labels, mostly) and then if you swipe to the right i need to display a MapView. If you then scroll to the left you see the labels again. What is the best way to create this?

Currently i have a UIScrollView with the width of the ViewController. Do i need to create 2 seperate .xib files, one for each page? And then load the xib files in the UIScrollView? Can someone point me in the right direction to create this? :)

Upvotes: 0

Views: 2648

Answers (1)

Vinay Jain
Vinay Jain

Reputation: 1661

This can be done with the help of two UIViews/XIBs, you have to set the pagingEnabled property of UIScrollView to true which does not let the UIScrollView to stop in between two UIViews.

Set the frame of your UIViews in such a way that they appear as pages in whole screen. Below is an example I just coded. And also set the contentSize of the UIScrollView. For better understanding see the code below

//Create a frame with size of the parent frame(fullscreen)
var firstFrame : CGRect = self.view.frame;

//this frame will be used for content size of UIScrollView
var bigFrame : CGRect = firstFrame

//since you need two views of full screen width, set the width of this frame as twice
bigFrame.size.width *= 2.0

//Create scrollview with frame "firstFrame" 
var scrollView : UIScrollView = UIScrollView(frame: firstFrame)
self.view.addSubview(scrollView)

var firstView : UIView = UIView(frame: firstFrame)
scrollView.addSubview(firstView)
firstFrame.origin.x = yourFirstView.frame.size.width
var secondView : UIView = UIView(frame: firstFrame)
scrollView.addSubview(secondView)
scrollView.pagingEnabled = true
scrollView.contentSize = bigFrame.size

Upvotes: 3

Related Questions