적당한 고통은 희열이다

- 댄 브라운 '다빈치 코드' 중에서

카테고리 없음

[Swift iOS] hide and show navigation bar when scroll

hongssup_ 2023. 7. 14. 18:04
반응형

스크롤 시 상단 네비게이션 바를 숨겼다가 보였다가 하도록 만드는 방법!

어려울 줄 알았는데 생각보다 간단하더라.

 

scrollViewWillBeginDecelerating(_:)

Tells the delegate that the scroll view is starting to decelerate the scrolling movement.

 

UIScrollViewDelegate 에서 상속받은 scrollViewWillBeginDecelerating 을 사용해서

스크롤을 내릴때는 컨텐츠를 더 많이 볼 수 있도록 상단 바를 없애고,

스크롤을 올릴 때 다시 상단바를 띄우도록 만들어주었다. 

func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
    if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 {
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    } else {
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
}

 

 

 

 

 

self.navigationController?.hidesBarsOnSwipe = true

이거는 해도 왜 안됨? 

 

 

 

참고 :

https://developer.apple.com/documentation/uikit/uiscrollviewdelegate/1619386-scrollviewwillbegindecelerating

https://stackoverflow.com/questions/40667985/how-to-hide-the-navigation-bar-and-toolbar-as-scroll-down-swift-like-mybridge

728x90
반응형