11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NavigationControllerをコードから利用するときのメモ

Last updated at Posted at 2014-02-03

色々と前提や基礎がまだまだなので大変ですが、とりあえずNavigationControllerをコードから利用して、シンプルなTableViewとDetailViewみたいな構成で作りたいものが作れたのでそのメモ。

前のビューに戻る

別のViewControllerを使って遷移した際、そのViewControllerの制御から元のViewControllerに戻りたい場合、遷移先(destinationViewcontroller的な)でdismissViewControllerAnimated:completion:メソッドを呼ぶと、前のViewControllerの処理に戻ることができる。

[self dismissViewControllerAnimated:YES completion:nil];

ナビゲーションバー周りの設定など

なにをどう設定したら、というのが結構分かりづらかったのでまとめメモ。

iOS7のナビゲーションバーの背景色を変更する

navigationContoroller.navigationBarの背景を変えてもダメ。
iOS7ではbarTintColorを変更する必要がある。
(iOS7以前ではプロパティがなくてエラーになるので注意)

nav-bg-setting.m
// ナビゲーションバーの背景色を赤に設定
UINavigationBar.appearance.barTintColor = UIColor.redColor;
nav-text-setting.m
// ナビゲーションバー上の「戻る」ボタンなどの色を変更する
UINavigationBar.appearance.tintColor = UIColor.whiteColor;

// タイトルの色を変更する
UINavigationBar.appearance.titleTextAttributes = @{NSForegroundColorAttributeName: UIColor.whiteColor};


// 以下の方法でもいける
viewController.navigationController.navigationBar.barTintColor = UIColor.redColor;
viewController.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: UIColor.whiteColor};

NavigationBarにボタンを設置する

最初勘違いしていたが、navigationItemに設定する項目は、親のUINavigationViewControllerのナビゲーションバーに項目を設定する、という意味。
どういうことかというと、以下のようにUINavigationControllerを生成して、それのnavigationItemにボタンを設定しても表示されません。
なぜなら、親(があるなら)のnavigationItemに設定しようとしているのであって、自分自身のnavigationBarではないからです。

UIViewController *aViewController = [[UIViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:aViewController];

// navigationControllerの`親の`navigationItemに設定しようとしている
navigationController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] init];
11
11
1

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
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?