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]
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の色とかの設定
// これはUIViewControllerのUInavigationControllerプロパティのnavigationBarに対して設定
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackOpaque];
// navigationBarの右側にボタンとか置いてみる
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(doneButtonTouched:)];
// UIViewのnavigationItemプロパティに対して setRightBarButtonItemとかする
[self.navigationItem setRightBarButtonItem:doneButton];
// これで戻るボタンが消える
[self.navigationItem setHidesBackButton:YES animated:NO];
// toolbarを表示するにはこれ
[self.navigationController setToolbarHidden:NO];
// toolbarの色の設定
// こっちはUINavigationContorllerのtoolbarプロパティに対して設定する
[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:)];
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// navigationBarの色とかの設定
// これはUIViewControllerのUInavigationControllerプロパティのnavigationBarに対して設定
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackOpaque];
// navigationBarの右側にボタンとか置いてみる
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(doneButtonTouched:)];
// UIViewのnavigationItemプロパティに対して setRightBarButtonItemとかする
[self.navigationItem setRightBarButtonItem:doneButton];
// これで戻るボタンが消える
[self.navigationItem setHidesBackButton:YES animated:NO];
// toolbarを表示するにはこれ
[self.navigationController setToolbarHidden:NO];
// toolbarの色の設定
// こっちはUINavigationContorllerのtoolbarプロパティに対して設定する
[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];
// flexibleSpaceを2回使うとかもできる
NSArray *items = [NSArray arrayWithObjects:actionButton,flexibleSpace, playButton, flexibleSpace,
cameraButton, nil];
// UIViewControllerのsetToolbarItemを呼ぶ
[self setToolbarItems:items];
}
みてお分かりの通り,UIViewControllerのプロパティに設定するのかと思えば,
UINavigationControllerに設定したりと,本当に複雑になってます.
HOMEに戻る