LoginSignup
1
0

More than 5 years have passed since last update.

AMScrollingNavBarで戻った時に上の位置がずれる

Last updated at Posted at 2019-01-20

はじめに

このAMScrollingNavBarを使ってNavigation barを隠し、表示領域を増やそうと考えCocoaPodsを使って入れた。しかし、navigationbarが思い通りに隠れず調べても分からなかったので書いてみた。

CocoaPods導入

自分で書いたものであるのでこの記事の参考文献なども見ていただくと深く知ることができるかもしれません。
CocoaPods導入

Podfileの変更

Podfileを変更する。Podfileの一部抜粋。

Podfile
# Comment the next line if you're not using Swift and don't want to use dynamic frameworks

pod 'AMScrollingNavbar'

use_frameworks!

変更される可能性があるのでリンクも添えて
AMScrollingNavBarの元リンク

いよいよ書き込み

  • ???.xcworkspaceを開く。
  • storyboardのNavigation ControllerのclassをScrolling Navigation Controllerにし、ModuleをAMScrollingNavBarにする。
  • その後???.swiftに書き込む。必要部分だけを抜粋
ViewController.swift
import AMScrollingNavbar
class ViewController: UIViewController{
override func viewDidLoad() {
        super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if let navigationController = navigationController as? ScrollingNavigationController {
            navigationController.followScrollView(tableview, delay: 50.0)
        }
    }
}

このときnavigation barを透明化するプログラムを書いていると正しく表示されないと書かれていたがそんなんことはなかった。透明化するプログラムは書いてもよかった。

ViewController.swift
 self.navigationController?.navigationBar.isTranslucent = true

失敗する例

ここに書いてある失敗する例は書かれていなかった気がする。
toolbarやnavigation barを隠すことができる元々のものがある。
そのコードを書いているとうまく動かない。

ViewController.swift
func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 {
            navigationController?.setToolbarHidden(true, animated: true)
        } else {
            navigationController?.setToolbarHidden(false, animated: false)
        }
    }

このコードを書いているとおそらくtoolbarの高さ分だけスクロールするたびに一番上の値が変わっていく。期待通りの結果は得られなくなる。

最後に

toolbarも一緒に変更したい場合は違うライブラリを使うしかないということがわかった。

参考文献

途中で紹介したAMScrollingNavBarの元リンク

1
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
1
0