I have a problem with UITableView. It won't hide the scroll indicator after:
1) scrolling fast
2) and then hitting the top or bottom of the table.
Here's a screenshot.
How can I make sure the scroll indicator hides correctly as expected?
Please note that bouncing is off. I also don't want to just hide the scroll indicator, I just want it to disappear as expected when scrolling stops at the top or the bottom.
EDIT: This problem seems to be caused by setting the view controller setting automaticallyAdjustsScrollViewInsets
to false
. It seems that the following 3 things need to be set to reproduce the problem:
1) the table view bounces needs to be off
2) the view controller setting automaticallyAdjustsScrollViewInsets
to false
(This is to fix a different problem where the scroll indicator does not look right at all)
3) The view of the UIViewController itself should not be the table view, the table view has to be a subview.
In viewDidLoad
that will look something like this:
self.view_table = [[UITableView alloc] initWithFrame:self.view.frame];
self.view_table.bounces = false;
self.automaticallyAdjustsScrollViewInsets = false;
Also, the content of the table view needs to be bigger than the height of its frame.
UITableView inherits from UIScrollView, so you'll need to use UIScrollView's properties:
Property: showsVerticalScrollIndicator
A Boolean value that controls whether the vertical scroll indicator is visible.
Take a look at the documentation .
Try this :
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
if (!scrollView.bounces) {
targetContentOffset->y = -1;//Scrollbar does not move here, because bounces is disabled, but scrollbar can hidden.
}
}
Do the Follwoing steps.
I have an answer based on the answer by 范亚楠.
In the UITableViewDelegate
: Objective-C :
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
if (!scrollView.bounces) {
if(targetContentOffset->y <= 1)
{
targetContentOffset->y = 0.01;
}
else if(targetContentOffset->y >= scrollView.contentSize.height - scrollView.height)
{
targetContentOffset->y = scrollView.contentSize.height - scrollView.height - 0.01;
}
}
}
Swift 4:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
guard !scrollView.bounces else {
return
}
if(targetContentOffset.pointee.y <= 1)
{
targetContentOffset.pointee.y = 0.01;
}
else if(targetContentOffset.pointee.y >= scrollView.contentSize.height - scrollView.height)
{
targetContentOffset.pointee.y = scrollView.contentSize.height - scrollView.height - 0.01;
}
}
If the table view is scrolling to the top or the bottom this code makes it stop very close but not quite at the end. This allows the scroll indicator to disappear.
In viewwillLoad() Make tableView.showsVerticalScrollIndicator = false
to disable the scroll indicators in the scroll action view in the tableView
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.