3254523
3254523

Reputation: 3026

Jerky behavior on UIView when hiding a UIStatusBar?

I have a view that is placed over a UIScrollView which moves according to the UIScrollView's content offset. I achieve this by adjusting the view's vertical constraint in scrollViewDidScroll :

topViewTopConstraint.constant = -scrollView.contentOffset.y - topView.frame.height

The topView moves great until the opaque status bar needs to become hidden, which is when it meets a certain threshold. In my VC's scrollViewDidScroll, i achieve it using the following code

if -scrollView.contentOffset.y <= thresholdY {
                self.statusBarHidden = true
            } else {
                self.statusBarHidden = false
            }
            self.setNeedsStatusBarAppearanceUpdate()

When the scrollView's content offset for Y reaches the thresholdY, the topView jerks.

I've put a log on the -scrollView.contentOffset.y and it seems to jump 20 pixels during the jerking. Any idea why this could be happening?

Upvotes: 2

Views: 300

Answers (2)

Jav Solo
Jav Solo

Reputation: 736

For anyone running into this issue in Swift today when rotating a scrollview within a VC, I fixed the 20px offset when rotating into landscape by calling scrollView.contentSize = size in the viewWillTransition like so

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        scrollView.contentSize = size
        scrollView.frame = CGRect(origin: .zero, size: size)
    }

Upvotes: 0

Arash
Arash

Reputation: 1316

Is the topView constraint based on the top guide line? The problem is that the top constraint is probably been defined based on the bottom of the top guide line which is bottom of status bar. When you hide the status bar that line moves. So when the offset meets your threshold it hides the status bar which then makes the if statement untrue which turns the status bar back on and so on.

You can probably define the top constraint in relation to the bottom guide line or something else that is constant.

Upvotes: 2

Related Questions