Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active May 25, 2018 22:57
Show Gist options
  • Save KentarouKanno/f061bfd4089a4c9dff1e to your computer and use it in GitHub Desktop.
Save KentarouKanno/f061bfd4089a4c9dff1e to your computer and use it in GitHub Desktop.
UINavigationController

UINavigationController

★ 先頭のViewControllerを取得

let topViewController = self.navigationController!.topViewController

★ 自分にぶら下がっているViewControllerの配列を取得、設定する

let vcArray = self.navigationController?.viewControllers


// ナビコンの順番を入れ替えて設定する
if var viewArray = self.navigationController?.viewControllers {
    swap(&viewArray[0], &viewArray[1])
    self.navigationController?.viewControllers = viewArray
}

★ ナビコンのタイトルを設定する

self.navigationItem.title = "Navi Title"

// ViewControllerの self.title = "Navi Title" でも同じことはできる

★ ViewControllerのNavigationBarを非表示にする

// 画面表示前に処理を記述
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.setNavigationBarHidden(true, animated: false)
}

★ ナビバーの左右のBarButtonItemを生成する

// Right
let rightBarButton = UIBarButtonItem(title: "right", style: .Plain, target: self, action: Selector("pushRightButton"))
self.navigationItem.rightBarButtonItem = rightBarButton

func pushRightButton() {
    print("pushRightButton!")
}

// Left
let leftBarButton = UIBarButtonItem(title: "Left", style: .Plain, target: self, action: Selector("pushLeftButton"))
self.navigationItem.leftBarButtonItem = leftBarButton

func pushLeftButton() {
    print("pushLeftButton!")
}

★ 戻るボタンを非表示にする

self.navigationItem.hidesBackButton = true

★ 遷移する画面、アニメーションの有無を指定して画面遷移する

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let nextVC = storyboard.instantiateViewControllerWithIdentifier("next")

self.navigationController?.pushViewController(nextVC, animated: true)

★ アニメーションの有無を指定して一つ前の画面へ戻る

self.navigationController?.popViewControllerAnimated(true)

let viewController = self.navigationController?.popViewControllerAnimated(true)
//=> 戻り値は現在のViewController

★ 戻りたいViewControllerとアニメーションの有無を指定して画面を戻る

self.navigationController?.popToViewController("戻りたいViewController", animated: false)

// Usage
if let viewArray = self.navigationController?.viewControllers {
    self.navigationController?.popToViewController(viewArray[0], animated: false)
}

★ アニメーションの有無を指定してナビゲーションの先頭画面まで遷移する

self.navigationController?.popToRootViewControllerAnimated(true)

★ ナビゲーションバーの表示、非表示を切り替える

self.navigationController?.navigationBarHidden = true

★ アニメーションの有無を指定して、ナビゲーションバーの表示、非表示を切り替える

self.navigationController?.setNavigationBarHidden(true, animated: true)

★ 現在表示中のViewControllerを取得する

let visibleVC = self.navigationController?.visibleViewController

★ 画面タップでナビバーを隠すかどうかの設定

self.navigationController?.hidesBarsOnTap = true

★ Delegateを設定する

// UINavigationControllerDelegateを継承
class ViewController: UIViewController, UINavigationControllerDelegate { }

// NavigationControllerのDelegateを設定する
self.navigationController?.delegate = self

★ NavigationControllerの遷移速度を調節する
※遷移が綺麗に見えないので検証が必要

let nextVC = storyboard?.instantiateViewControllerWithIdentifier("vc")

let transition = CATransition()
transition.duration = 0.6
transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
self.navigationController!.view.layer.addAnimation(transition, forKey: nil)
self.navigationController?.pushViewController(nextVC!, animated: true)
// ナビゲーションの背景色を設定
UINavigationBar.appearance().barTintColor = UIColor.red
        
// ナビゲーションアイテムのカラーを設定する
UINavigationBar.appearance().tintColor = UIColor.white
        
// ナビゲーションタイトルのカラーを設定する
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]

★ NavigationBarを透明にする

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    // ナビゲーションを透明にする処理
    self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController!.navigationBar.shadowImage = UIImage()
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    
    // 透明にしたナビゲーションを元に戻す処理
    self.navigationController!.navigationBar.setBackgroundImage(nil, for: .default)
    self.navigationController!.navigationBar.shadowImage = nil
}

UINavigationControllerDelegate

★ 画面が表示される直前に呼ばれる

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
        
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment