Is there any way that i can make a UIScrollView scroll like a UIPageController or like a page because currently when we scroll UIScrollView its like free..
Asked
Active
Viewed 118 times
0
-
Yes, the scrollview supports paging, please check https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/UIScrollView_pg/ScrollViewPagingMode/ScrollViewPagingMode.html – thelaws Mar 29 '16 at 19:52
2 Answers
2
As Heximal as mentioned scrollview has property pagingEnabled
which you need to enable to make pagination in scrollview
Your sample code can be like this:
@IBOutlet weak var scrollPagination: UIScrollView!
var numberOfPages:CGFloat = 5
override func viewDidLoad() {
super.viewDidLoad()
scrollPagination.pagingEnabled = true
scrollPagination.contentSize = CGSizeMake(numberOfPages * scrollPagination.frame.size.width, scrollPagination.frame.size.height)
for i in 0...Int(numberOfPages) {
let tmpLabel: UILabel = UILabel(frame: CGRectMake(CGFloat(i) * self.view.bounds.width + 20, 20, 120, 20))
tmpLabel.textAlignment = .Center
tmpLabel.text = "This is page \(i)"
scrollPagination.addSubview(tmpLabel)
}
}
Another code reference for your code: https://stackoverflow.com/a/29300300/4557505
0
-
Link-only answers become meaningless in the event that the link breaks. At least include an outline of the solution in tje text; ideally code. – Nicolas Miari Mar 29 '16 at 22:27
-
1Thanks Nicolas, you're 100% right I will take it into account. I faced myself this situation with broken links several times. I did upvote the answer of @Pyro – heximal Mar 30 '16 at 08:59