LoginSignup
0
0

More than 1 year has passed since last update.

スクロールした分だけ、NavigationBarをせり上げる

Last updated at Posted at 2021-11-01

NavigationBarをスクロールしたときにNavigationBarを非表示する

表題の件を実現するには、apple側で実装されている下記プロパティーを利用する

上記の方法ですと、スワイプでNavigationBarを非表示にすることはできますが、スワイプさせた分だけ、NavigationBarをせり上げることや、非表示にしていくことはできません🤦🏽‍♂️ OMG😇

そこで、下記のライブラリーを用いることでそれを実現できます。

このライブラリーはUIScrollViewDelegateを用いていないので、干渉する事なく動作します。

ClimbBar

Overview

UITableViewやUIWebViewなどのスクロール可能な要素を持つビューを拡張できるiOSライブラリ。

スクロール可能なビューをスワイプすると、スワイプされた値が「emit」から返されます。
したがって、スクロール可能なビューをスワイプすると、それに応じて「navigationBar」が上昇します。

github page build status
result build

Gif

Cocoapods

pod 'ClimbBar'

git clone

SSH経由:定期的に直接コミットを行う予定の場合、SSHを介したクローン作成により、エクスペリエンスが向上する可能性があります(SSHキーをGitHubにアップロードする必要があります)。

$ git clone git@github.com:keisukeYamagishi/ClimbBar.git

https経由 ソースを読み取り専用としてチェックアウトする場合は、HTTPSが最適です。

$ git clone https://github.com/keisukeYamagishi/ClimbBar.git

Sample code

override func loadView() {
        super.loadView()
        title = "ViewController"
        let statusBarHeight = UIApplication.statusBarHeight
        let toHeaderBottom = statusBarHeight + (navigationController?.barHeight ?? 0)
        // 下記のクラスに範囲を指定することでその範囲内でのみスクロール値が変動するようになります
        let conf = Configuration(range: statusBarHeight ... toHeaderBottom)

        climbBar = ClimbBar(configurations: conf,
                            scrollable: tableView)

        climbBar.emit { [weak self] state in
            guard let self = self else { return }
            self.navigationController?.setAlpha(alpha: CGFloat(state.progress))
            let navigtionFrame = CGRect(x: 0,
                                        y: state.originY,
                                        width: self.view.frame.size.width,
                                        height: 44)
            self.navigationController?.navigationBar.frame = navigtionFrame
        }
    }
extension UIApplication {
    static var statusBarFrame: CGRect {
        UIApplication.shared.connectedScenes
            .filter { $0.activationState == .foregroundActive }
            .map { $0 as? UIWindowScene }
            .compactMap { $0 }
            .first?
            .windows
            .filter { $0.isKeyWindow }.first?
            .windowScene?
            .statusBarManager?
            .statusBarFrame ?? .zero
    }

    static var statusBarHeight: CGFloat {
        statusBarFrame.size.height
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0