Reputation: 28756
Is it possible to bounce a UITableView
on the bottom side, but not the top? If so, please show me code.
Upvotes: 13
Views: 9946
Reputation: 425
func scrollViewDidScroll(_ scrollView: UIScrollView) {
//Check to make sure this only applies to tableview
//and not other scrollviews inside your View/ViewController
if scrollView.superclass == UITableView.self {
//bounce will only be true if contentOffset.y is higher than 100
//I set 100 just to make SURE, but you can set >= 0 too
scrollView.bounces = scrollView.contentOffset.y > 100
}
}
Upvotes: 1
Reputation: 5939
For swift 4
extension YourViewController: UIScrollViewDelegate{
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == tableView {
let a = scrollView.contentOffset
if a.y <= 0 {
scrollView.contentOffset = CGPoint.zero // this is to disable tableview bouncing at top.
}
}
}
}
Upvotes: 8
Reputation: 6158
I have also encountered this problem and my solution is:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y > 100) {
scrollView.bounces = NO;
}else{
scrollView.bounces = YES;
}
}
I use this code in my project and it work well.
Upvotes: 4
Reputation: 1129
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y <= 0) {
scrollView.contentOffset = CGPointZero;
scrollView.showsVerticalScrollIndicator = NO;
} else {
scrollView.showsVerticalScrollIndicator = YES;
}
}
@Liron Berger 's solution will cause VerticalScrollIndicator still showing, just set showsVerticalScrollIndicator
to NO
can solve this problem
Upvotes: 0
Reputation: 4522
In swift 2.2:
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView.contentOffset.y <= 0 {
scrollView.contentOffset = CGPointZero
}
}
Upvotes: 0
Reputation: 456
Instead of changing the bounces
property, I added that to the UIScrollViewDelgate
method:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y<=0) {
scrollView.contentOffset = CGPointZero;
}
}
Upvotes: 44
Reputation: 8444
It should be possible by observing UITableview's contentOffset property.
[_tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:NULL];
. . and preventing the offset from going negative.
Upvotes: 4
Reputation: 10195
You could try using the UIScrollViewDelegate
methods to detect dragging direction and adjust the bounces
property appropriately?
Upvotes: 2