(iOS)備忘録 ステータスバー、ナビゲーションバーをいじる
備忘録
アプリ作る毎にどうやったっけってなるので備忘録を。
まあ、割とこの手の記事はググれば見つかりますが。
ステータスバー
ここではステータスバーを明るくしてみる。
1. Info.plistで View controller-based status bar appearance を NO にする
↑のハイライトな部分
2. UIApplicationのsetStatusBarStyle:animated:をつかう
AppDelegateとかで、以下の関数をよぶ。
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
ナビゲーションバー
タイトルがテキストの場合
こんなナビゲーションバーをつくる。
適当にハロウィン。
ViewControllerに実装。
- (void)viewDidLoad { [super viewDidLoad]; // ナビゲーションバーの背景色 self.navigationController.navigationBar.barTintColor = [UIColor blackColor]; // バータイトルの色 self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]}; // バーボタンの色 self.navigationController.navigationBar.tintColor = [UIColor yellowColor]; // バータイトル self.title = @"HELLOWEEN"; // バーボタン self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"お菓子" style:UIBarButtonItemStylePlain target:self action:nil]; }
タイトルが画像のとき
お次はこんなナビゲーションバー
タイトルで画像を使ってます。
まずは適当に画像を用意。
↑透過MAXなんでこのまま見るとタダのカボチャ。
こちらもViewControllerに実装。
- (void)viewDidLoad { [super viewDidLoad]; // ナビゲーションバーの背景色 self.navigationController.navigationBar.barTintColor = [UIColor blackColor]; // バーボタンの色 self.navigationController.navigationBar.tintColor = [UIColor yellowColor]; // バーボタン self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"お菓子" style:UIBarButtonItemStylePlain target:self action:nil]; // タイトルに貼付けたいイメージ UIImage* image = [UIImage imageNamed:@"titlebar.png"]; // イメージのサイズを調節 CGSize viewSize = CGSizeMake(image.size.width, image.size.height); CGFloat imageHeight = 30; CGSize titleImageSize = CGSizeMake(viewSize.width * (imageHeight / viewSize.height), imageHeight); // UIImageViewをつくる UIImageView* titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, titleImageSize.width, imageHeight)]; titleImageView.backgroundColor = [UIColor clearColor]; titleImageView.image = image; [titleImageView setContentMode:UIViewContentModeScaleAspectFit]; // 縦横比を維持しつつImageViewに画像をセット // UIViewにUIImageViewをのっける UIView* bgView = [[UIView alloc] initWithFrame:titleImageView.frame]; bgView.backgroundColor = [UIColor clearColor]; [bgView addSubview:titleImageView]; //UINavigationBarのタイトルに画像を設定。 self.navigationItem.titleView = bgView; }