現象:在 UINavigationController 中,從頁面 A push 到頁面 B(B 含 UIScrollView),點擊返回鈕回到 A 時,B 頁的 scrollView 內容會在 A 頁殘留約 1 秒才消失。
頁面 B 為了讓 scrollView 內容超出邊界顯示(如下拉刷新彈性效果、橫向卡片輪播),設定了 scrollView.clipsToBounds = false。轉場動畫期間,系統截圖/渲染邏輯未正確處理超出邊界的內容,導致殘影。
在頁面 B 的 viewWillDisappear(_:) 中將 clipsToBounds 恢復為 true,轉場結束後若需保持原行為,可在 viewWillAppear(_:) 再改回 false。
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
scrollView.clipsToBounds = true // 關鍵:轉場前裁切超出內容
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
scrollView.clipsToBounds = false // 還原業務需求
}
clipsToBounds = true。核心原則:**轉場/推出畫面前,確保可滾動視圖的 clipsToBounds 為 true,避免 GPU 殘留渲染緩衝區內容。**