色々と前提や基礎がまだまだなので大変ですが、とりあえずNavigationControllerをコードから利用して、シンプルなTableViewとDetailViewみたいな構成で作りたいものが作れたのでそのメモ。
前のビューに戻る
別のViewControllerを使って遷移した際、そのViewControllerの制御から元のViewControllerに戻りたい場合、遷移先(destinationViewcontroller的な)でdismissViewControllerAnimated:completion:
メソッドを呼ぶと、前のViewControllerの処理に戻ることができる。
[self dismissViewControllerAnimated:YES completion:nil];
ナビゲーションバー周りの設定など
なにをどう設定したら、というのが結構分かりづらかったのでまとめメモ。
iOS7のナビゲーションバーの背景色を変更する
navigationContoroller.navigationBarの背景を変えてもダメ。
iOS7ではbarTintColor
を変更する必要がある。
(iOS7以前ではプロパティがなくてエラーになるので注意)
// ナビゲーションバーの背景色を赤に設定
UINavigationBar.appearance.barTintColor = UIColor.redColor;
// ナビゲーションバー上の「戻る」ボタンなどの色を変更する
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];