LoginSignup
15
15

More than 5 years have passed since last update.

[iOS7]特定のViewControllerで、特定条件時にステータスバーを非表示にする

Last updated at Posted at 2014-02-24

iOS7の特定のViewControllerで、特定条件時にステータスバーを非表示にする方法です。

info.plistの設定

info.plist に ”View controller-based status bar appearance” という項目を追加し、値を ”YES” に。これでViewControllerごとにステータスバーの表示を制御可能になる。

ViewController.m

ステータスバーの表示を制御したいViewControllerにて、”statusbarHidden” のようなステータスバーの表示/非表示のフラグ用のプロパティを用意しておく。

ViewController.m
@interface ViewController ()

@property BOOL statusbarHidden;

@end

さらに ”prefersStatusBarHidden" というメソッドを実装し、戻り値を "_statusbarHidden" とする。

ViewController.m
- (BOOL)prefersStatusBarHidden {
    return _statusbarHidden;
}

あとは状況に応じて、"_statusbarHidden" に "YES" か "NO" をセットし、ステータスバーの画面更新メソッド "setNeedsStatusBarAppearanceUpdate" を呼べばOK。以下はiPhoneを横向きにした場合にステータスバーを非表示にする例。

ViewController.m
- (void)viewWillLayoutSubviews {
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
    switch (interfaceOrientation) {
        case UIInterfaceOrientationPortrait:
            _statusbarHidden = NO;
            [self setNeedsStatusBarAppearanceUpdate];
            break;
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            _statusbarHidden = YES;
            [self setNeedsStatusBarAppearanceUpdate];
            break;
        default:
            break;
    }
}
15
15
0

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
15
15