UINavigationControllerのnavigationBarとtoolbar | 初心者がiPhoneアプリを作るブログ

初心者がiPhoneアプリを作るブログ

初心者がiPhoneアプリを作るブログです.
入門レベルですので開発初心者にも,できるだけ分かるように丁寧に説明していきます(多分).

完全に落とし穴である,UINavigationControllerですが,今回は
navigationBarとtoolbarについて見ていきましょう!!

UINavigationControllerを使っていると,
navigationBarやtoolbarをカスタマイズする場面が多くなると思います.

しかし,このカスタマイズが意外と厄介なんです!!
なぜなら!!navigationBarやtoolbarの設定はUIViewController内で行うからです!!

そこでサンプルコードを載せますのでみなさん研究して下さい笑
↑説明する気ゼロ

AppDelegate.m
// Override point for customization after application launch.
CustomViewController *controller = [[CustomViewController alloc] init];
UINavigationController *rootViewController = [[UINavigationController alloc]

                                              initWithRootViewController:controller];
self.window.rootViewController = rootViewController;



CustomViewController.m : viewDidLoad
- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
    // navigationBarの色とかの設定
    // これはUIViewControllerUInavigationControllerプロパティのnavigationBarに対して設定
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackOpaque];
    // navigationBarの右側にボタンとか置いてみる
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
                                   initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                   target:self
                                   action:@selector(doneButtonTouched:)];
    // UIViewnavigationItemプロパティに対して setRightBarButtonItemとかする
    [self.navigationItem setRightBarButtonItem:doneButton];
    // これで戻るボタンが消える
    [self.navigationItem setHidesBackButton:YES animated:NO];
    
    // toolbarを表示するにはこれ
    [self.navigationController setToolbarHidden:NO];
    // toolbarの色の設定
    // こっちはUINavigationContorllertoolbarプロパティに対して設定する
    [self.navigationController.toolbar setBarStyle:UIBarStyleBlackTranslucent];
    // toolBarに置くボタンとかつくる
    UIBarButtonItem *cameraButton = [[UIBarButtonItem alloc]
                                      initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
                                      target:self
                                      action:@selector(cameraButtonTouched:)];
    UIBarButtonItem *playButton = [[UIBarButtonItem alloc]
                                   initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
                                   target:self
                                   action:@selector(playButtonTouched:)];
    UIBarButtonItem *actionButton = [[UIBarButtonItem alloc]
                                     initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                     target:self
                                     action:@selector(actionButtonTouched:)];


    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]
                                      initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                      target:nil
                                      action:nil];
    // flexibleSpace2回使うとかもできる
    NSArray *items = [NSArray arrayWithObjects:actionButton,flexibleSpace, playButton, flexibleSpace,
cameraButton,
nil];
    // UIViewControllersetToolbarItemを呼ぶ
    [self setToolbarItems:items];

}


みてお分かりの通り,UIViewControllerのプロパティに設定するのかと思えば,
UINavigationControllerに設定したりと,本当に複雑になってます.

HOMEに戻る