Lag / Screen Freezes when you hide the status bar dynamically as a scroll (Swift 3)

I am currently using Scroll View with different pages. I am trying to hide the Status Bar on one specific page. There is a really annoying lag , and the screen freezes every time the Status Bar is about to appear / disappear again.

This is my approach:

First of all, I defined two variables

 private var currentPage = 0 private var isStatusBarPreferablyHidden = true 

Then I used two functions from the scroll delegate to call setNeedsStatusBarAppearanceUpdate when the user moved to the first page

 func scrollViewDidScroll(_ scrollView: UIScrollView) { let currentScrollPosition : CGFloat = self.navigation.contentOffset.x / self.navigation.frame.size.width currentPage = lroundf(Float(currentScrollPosition)) } func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) { if currentPage == 0 { isStatusBarPreferablyHidden = true } else { isStatusBarPreferablyHidden = false } self.setNeedsStatusBarAppearanceUpdate() } 

Finally, I redefined the status bar functions to update the appearance of the status bar.

 override var prefersStatusBarHidden: Bool { return isStatusBarPreferablyHidden } override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation { return UIStatusBarAnimation.none } 
+5
source share
1 answer

Remove scrollViewWillBeginDecelerating . Try:

 func scrollViewDidScroll(_ scrollView: UIScrollView) { let currentScrollPosition : CGFloat = scrollView.contentOffset.x / scrollView.frame.size.width let page = lroundf(Float(currentScrollPosition)) if page != currentPage { currentPage = page if currentPage == 0 { isStatusBarPreferablyHidden = true } else { isStatusBarPreferablyHidden = false } self.setNeedsStatusBarAppearanceUpdate() } } 
0
source

Source: https://habr.com/ru/post/1260670/


All Articles