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