iPhone5対応してたらTabbarが動かない問題
iPhone5で縦長になる関係で対応していたところ
縦長splash画像を突っ込んでOKかと思ったら
Tagbarが動かない・・
調べていくと
[window addSubview:tabBarController.view]; [window makeKeyAndVisible];
を
window.rootViewController = tabBarController; window.frame = [[UIScreen mainScreen] bounds]; [window makeKeyAndVisible];
という記事は良く見つかる。
自分のプロジェクトはxcode3時代からのもので
// Add the tab bar controller's current view as a subview of the window // [window addSubview:tabBarController.view];
だけだったんだけど、
上記の例を行なってみるとTabすら消えてしまうだけでうまくいかない
xcodeでtabbarの新プロシェクトを作成してコードリーディングしたところ
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. UIViewController *viewController1, *viewController2; if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil]; viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil]; } else { viewController1 = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPad" bundle:nil]; viewController2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController_iPad" bundle:nil]; } self.tabBarController = [[UITabBarController alloc] init]; self.tabBarController.viewControllers = @[viewController1, viewController2]; self.window.rootViewController = self.tabBarController; [self.window makeKeyAndVisible]; return YES;
ってな感じでコードでTabBarをWindowにつっこんでるのが見つかった
自分のプロジェクトはInterfaceBuilderでガリガリ組んだので
これを行うのはむずい。。
そもそもtabBarControllerの実体にはすでに組み込まれ済みんなんだから
削りまくって以下のようなコードにしてみたところ動いた!
// Add the tab bar controller's current view as a subview of the window // //[window addSubview:tabBarController.view]; self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.rootViewController = tabBarController; [self.window makeKeyAndVisible];
ただし、ios6しか動かなくなるので
OS識別したほうが良い様子・・
float version = [[[UIDevice currentDevice] systemVersion] floatValue]; // 分岐 if(version < 6.0f){ //旧OS [window addSubview:tabBarController.view]; } else{ // iOS6〜の処理 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.rootViewController = tabBarController; [self.window makeKeyAndVisible]; }
他にも問題山積だけど進めば気持ちもだいぶ楽になるね