點擊狀態(tài)欄 scrollView 不能返回頂部

現(xiàn)在好多 app 都采用了多個 tableViewcollectionView 放到 scrollView 左右滑動來切換不同頁面的設(shè)計,這時候就會導(dǎo)致系統(tǒng)默認(rèn)的點擊狀態(tài)欄使當(dāng)前 window 最上層 scrollView (或其子類)滾動到最頂部的功能屏蔽,想要恢復(fù)這個功能大概有兩種方法:

  1. 把當(dāng)前 window 最上層的 scrollView (或其子類)的 scrollsToTop 屬性設(shè)為 true ,把當(dāng)前控制器的其他所有 scrollView (或其子類)的 scrollsToTop 屬性設(shè)為 false
  • AppDelegate 類中通過監(jiān)聽點擊事件,如果點擊到 statusBar 區(qū)域,則發(fā)出通知(NSNotification),(其他相關(guān)頁面需要先監(jiān)聽這個通知),其他頁面在收到通知的時候把當(dāng)前 window 最上層的 scrollView (或其子類)的 contentOffet.y 設(shè)為初始狀態(tài)
private typealias TouchStatusAction = AppDelegate
extension TouchStatusAction {
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        let touchLocation = event!.allTouches?.first!.location(in: self.window)
        let statusBarFrame = UIApplication.shared.statusBarFrame
        if statusBarFrame.contains(touchLocation!) {
            self.statusBarTouchedAction()
        }
        
    }
    
    func statusBarTouchedAction() {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "statusBarTouchedNotification"), object: nil)
    }
}

附錄:

  • 文檔中對 scrollsToTop 的解釋
    // When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its delegate does not return NO from `shouldScrollViewScrollToTop`, and it is not already at the top.
    // On iPhone, we execute this gesture only if there's one on-screen scroll view with `scrollsToTop` == YES. If more than one is found, none will be scrolled.
    open var scrollsToTop: Bool // default is YES.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容