[英]scrolling/resizing UITableView
我會講到重點。
我有一個UIViewController,其中有兩個子視圖。 頂部的是一個自定義的UIView(下面將其稱為HeaderView),而底部的是一個UITableView。
我已經在InterfaceBuilder中對其進行了設置,以便HeaderView的左側,頂部和右側的邊距為0,並且其高度固定。 UITableView在所有面的正下方,且頁邊距為0。
我的目標是實現一種行為,當我開始滾動UITableView的內容時,HeaderView將開始縮小,並且UITableView會變得更高而無需滾動。 這應該一直進行到HeaderView達到最小高度為止。 之后,UITableView應該開始正常滾動。 向下滾動時,效果應相反。
我最初是使用UIScrollView而不是UITableView來開始的,並且已經達到了預期的結果。 方法如下:
將UIScrollView連接到插座
@IBOutlet weak var scrollView: UIScrollView!
在控制器的viewDidLoad()
方法中設置UIScrollViewDelegate self.scrollView.delegate = self
並聲明UIViewController符合協議
在UIScrollView滾動時進行攔截:
func scrollViewDidScroll(_ scrollView: UIScrollView) { self.adjustScrolling(offset: scrollView.contentOffset.y, scrollView: scrollView) }
在我的adjustScrolling(offset:scrollView:)
方法中,“魔術”發生了
現在讓我們看看這種方法會發生什么。
private func adjustScrolling(offset: CGFloat, scrollView: UIScrollView) {
// bind value between 0 and max header scroll
let actualOffset: CGFloat = offset < 0 ? 0 : (offset >= self.maxHeaderScroll ? self.maxHeaderScroll : offset)
// avoid useless calculations
if (actualOffset == self.currentOffset) {
return
}
/**
* Apply the vertical scrolling to the header
*/
// Translate the header up to give more space to the scrollView
let headerTransform = CATransform3DTranslate(CATransform3DIdentity, 0, -(actualOffset), 0)
self.header.layer.transform = headerTransform
// Adjust header's subviews to new size
self.header.didScrollBy(actualOffset)
/**
* Apply the corrected vertical scrolling to the scrollView
*/
// Resize the scrollView to fill all empty space
let newScrollViewY = self.header.frame.origin.y + self.header.frame.height
scrollView.frame = CGRect(
x: 0,
y: newScrollViewY,
width: scrollView.frame.width,
height: scrollView.frame.height + (scrollView.frame.origin.y - newScrollViewY)
)
// Translate the scrollView's content view down to contrast scrolling
let scrollTransform = CATransform3DTranslate(CATransform3DIdentity, 0, (actualOffset), 0)
scrollView.subviews[0].layer.transform = scrollTransform
// Set bottom inset to show content hidden by translation
scrollView.contentInset = UIEdgeInsets(
top: 0,
left: 0,
bottom: actualOffset,
right: 0
)
self.currentOffset = actualOffset
}
如果我沒有忘記任何內容,這應該足以實現所需的效果。 讓我分解一下:
actualOffset
0之間結合它self.MaxHeaderScroll
這僅僅是67(我認為,它是動態計算的,但這並不重要) actualOffset
並沒有發生變化,那么我就不會麻煩地進行任何更改。 這避免了一些無用的計算。 CATransform3DTranslate
沿y軸actualOffset
,而負的actualOffset
。 self.header.didScrollBy(actualOffset)
以便HeaderView可以在內部應用一些可視更改。 但這並沒有掩蓋問題。 actualOffset
量相同,以對比滾動。 對於我想要實現的正確視覺效果而言,這是必不可少的。 如果我不這樣做,scrollView仍會正確調整大小,但是內容將立即開始滾動,這是我不希望的。 僅應在HeaderView達到最小高度后才開始滾動。 actualOffset
供以后比較 正如我所說,這很好。 當我從UIScrollView切換到UITableView時出現問題。 由於UITableView繼承自UIScrollView,因此我認為它會起作用。
唯一不起作用的代碼是數字6。我真的不知道出了什么問題,所以我只列出我發現和/或注意到的所有內容。 希望有人能夠幫助我。
scrollView.subviews[0]
引用一個視圖,其中包含所有內容。 當我更改為UITableView時,此子視圖似乎是UITableViewWrapperView
類型,找不到任何文檔,XCode也不將其識別為有效類。 這已經令人沮喪。 dequeueReusableCell(withIdentifier:for:)
實例化單元格,並且UITableView認為頂部的單元格實際上並不可見。 我無法解決此問題。 self.tableView.tableHeaderView
設置為actualOffset
高度的UIView進行對比度滾動,但這產生了怪異的效果,即單元格無法正確滾動,並且當UITableView返回初始位置時,會出現間隙在上面。 也沒有任何線索。 我知道這里有很多東西,所以請隨時詢問更多詳細信息。 先感謝您。
我最近做了這樣的事情,所以這是我如何實現的:
使一個具有高度約束常量的UIView並將其鏈接到您的視圖/ VC,將UITableview約束到UIView后面的VC全屏視圖。
現在,將UITableViews contentInset頂部設置為“ headerView”的起始高度,在scrollViewDidScroll中,調整常量,直到標題的高度最小為止。
如果只運行它,則藍色區域是您的“標題”,而彩色行則是任何單元格。 您可以在藍色區域中自動布局任何內容,它應該會自動調整大小和所有內容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.