我在ScrollView超級視圖中有一個UIView。我希望這個UIView能夠堅持到頂端,並在用戶滾動時停留在那裏。Swift:如何在滾動時將視圖錨定到頂部
請注意,它應該在屏幕中間開始時分別向上滾動。
我見過很多的問題和答案,但他們沒有解決我的問題
iOS: Add subview with a fix position on screen
Simple way to change the position of UIView?
我在scrollViewDidScroll
方法的代碼
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y > anchor.frame.origin.y {
var fixedFrame:CGRect = self.anchor.frame
fixedFrame.origin.y = (self.navigationController?.navigationBar.frame.height)!
self.anchor.frame = fixedFrame
}
}
是在它在開始前這一觀點,或者是別的地方,有它上面其他的看法? – Larme
它位於scrollView的中間,受約束約束,也許這是一個問題? – DCDC
不應該,只是如果它已經在頂部,最簡單的方法就不必將它放在滾動視圖中,並從該粘滯視圖開始滾動視圖的框架。 – Larme