LoginSignup
11
12

More than 5 years have passed since last update.

ナビゲーションバーの戻るボタンの文言をstoryboardで変える

Posted at

iOSアプリのナビゲーションバーに自動で表示される「戻る」ボタンは、遷移前のViewControllerのタイトルが自動で使われるが、これを自由に変更したい時がある。

Storyboardで変更したい

そういう時は、Storyboardで"遷移前"のNavigation ItemのBack Buttonに記載すればいい。

図はStackOverflowにある
http://stackoverflow.com/questions/24693248/ios-storyboard-back-button/24695475#24695475

コードで書きたい

これをどうしてもコードで書きたいと、Storyboard(IntefaceBuilder)という便利でフレンドリーなやつがあるのにそれを使わずに条件分岐によって書き換えたい場合や、もしくは特殊な性癖のせいで常人には理解できない理由がある人は次のようにするとコードで変えられる。

// 遷移前のViewControllerだよっ...!!!
override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.backBarButtonItem = UIBarButtonItem(title: "戻ります", style: .Plain, target: nil, action: nil)
}

遷移後の画面の表示なのに遷移前のnavigationItemに設定するというのは基本的には遷移前のタイトルを使うからなんだろうなあ、と覚えると2日くらいは覚えていられる。

11
12
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
11
12