Stopping ScrollView Going Above & Below The Bounds

人盡茶涼 提交于 2019-12-24 14:28:42

问题


I created a UIScrollView in a ViewController and added UIView & UITableView as siblings of ViewController.

 var pageSize = view.bounds.size

 // MiniQuestion: I disabled vertical indicators but it caused a gap for the width
 // of the indicator, so I added the gap manually. Is it okay behaviour?
 pageSize.width += 5


 MyView.backgroundColor = UIColor.redColor()
 MyTableView.backgroundColor = UIColor.blueColor()

 let pagesViews = [MyView , MyTableView]
 let numberOfPages = pagesViews.count

 for (pageIndex,page) in pagesViews.enumerate(){
    page.frame = CGRect(origin: CGPoint(x:0 , y:CGFloat(pageIndex) * pageSize.height), size: pageSize)
    ScrollView.addSubview(page)
 }

 ScrollView.contentSize = CGSize(width: pageSize.width, height: pageSize.height * CGFloat(numberOfPages))

It's working nice. Except, even though I have 2 pages and added constraints to ScrollView, it's going above the top page (when I pull more) and below the end of tableview page (when I push more). However I want to scroll it until the end of the tableview and stuck at the bound of the top.

Normally they look fine... However,

If I pull beyond top:

If I push below bottom:

How can I prevent this behaviour?


回答1:


If you don't want to enable user to pull it beyond the bounds of scroll view then:

ScrollView.bounces = false


来源:https://stackoverflow.com/questions/36538687/stopping-scrollview-going-above-below-the-bounds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!