![](/img/trans.png)
[英]Get a UITableView to scroll up from the bottom / snap to the bottom, not the top
[英]UITableView scroll indicator won't hide at top or bottom
我有UITableView的問題。 它之后不會隱藏滾動指示器:
1)快速滾動
2)然后擊中桌子的頂部或底部。
這是一個截圖。
如何確保滾動指示器按預期正確隱藏?
請注意,彈跳已關閉。 我也不想隱藏滾動指示器,我只是希望它在滾動停止在頂部或底部時按預期消失。
編輯:這個問題似乎是由於將視圖控制器設置automaticallyAdjustsScrollViewInsets
設置為automaticallyAdjustsScrollViewInsets
為false
。 似乎需要設置以下3件事來重現問題:
1)表視圖彈跳需要關閉
2)視圖控制器automaticallyAdjustsScrollViewInsets
設置調整automaticallyAdjustsScrollViewInsets
為false
(這是為了解決滾動指示器看起來不正確的另一個問題)
3)UIViewController本身的視圖不應該是表視圖,表視圖必須是子視圖。
在viewDidLoad
,它看起來像這樣:
self.view_table = [[UITableView alloc] initWithFrame:self.view.frame];
self.view_table.bounces = false;
self.automaticallyAdjustsScrollViewInsets = false;
此外,表格視圖的內容需要大於其框架的高度。
UITableView繼承自UIScrollView,因此您需要使用UIScrollView的屬性:
Property: showsVerticalScrollIndicator
A Boolean value that controls whether the vertical scroll indicator is visible.
看一下文檔 。
嘗試這個 :
-(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.
}
}
做Follwoing步驟。
根據范亞楠的回答,我得到了答案。
在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;
}
}
}
斯威夫特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;
}
}
如果表視圖滾動到頂部或底部,則此代碼使其停止非常接近但不完全結束。 這允許滾動指示器消失。
在viewwillLoad()中,使tableView.showsVerticalScrollIndicator = false
禁用tableView中滾動操作視圖中的滾動指示器
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.