2015年2月2日月曜日

[iOS]iOS7以降のStatusbar、Navigationbar、Toolbarのメモ

//ProjectAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 8.0) {
        //iOS8でtoolbar naigationbarの透過をオフにする
        //iOS7ではsetTranslucentでBOOLを受け付けないのでクラッシュする
        [[UINavigationBar appearance] setTranslucent:NO];
        [[UIToolbar appearance] setTranslucent:NO];
    }
    return YES;
}

//ViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
   
    isHidingStatusBar = YES;
    //iOS7 ステータスバーの表示/非表示切り替え
    //navigationBarHidden toolbarHiddenを呼び出した時にも呼ばれる
    [self setNeedsStatusBarAppearanceUpdate];
    //iOS7
    self.navigationController.navigationBarHidden = YES;
    self.navigationController.toolbarHidden = YES;

    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar setTranslucent:YES];
    [self.navigationController setNavigationBarHidden:NO];
    
    [self.navigationController.toolbar setBarTintColor:[UIColor blackColor]];
    [self.navigationController.toolbar setTintColor:[UIColor whiteColor]];
    [self.navigationController.toolbar setTranslucent:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //[[[UIApplication sharedApplication] keyWindow] tintColor]は変更がない限りデフォルトカラー
    [self.navigationController.navigationBar setBarTintColor:[[[UIApplication sharedApplication] keyWindow] tintColor]];
    [self.navigationController.navigationBar setTintColor:[[[UIApplication sharedApplication] keyWindow] tintColor]];
    
    [self.navigationController.toolbar setBarTintColor:[[[UIApplication sharedApplication] keyWindow] tintColor]];
    [self.navigationController.toolbar setTintColor:[[[UIApplication sharedApplication] keyWindow] tintColor]];
}


//setNeedsStatusBarAppearanceUpdateで呼び出される
- (BOOL)prefersStatusBarHidden {
    //ステータスバーの表示/非表示をBoolで指定
    return isHidingStatusBar;
}

//setNeedsStatusBarAppearanceUpdateで呼び出される
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
{
    //アニメーションの設定
    return UIStatusBarAnimationFade;
}