UITabBar を隠すコードを自分のアプリに実装してみる

画面の回転時に TabBar を消したいので、自分は - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration にこのコードを入れることにした。
willAnimateRotationToInterfaceOrientation:duration に入れればアニメーション中に一緒に変更できるので面倒なくて楽チン。
コードはこんな感じ。

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
	if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft ||
	interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
		self.tabBarController.tabBar.hidden = YES;
		for(UIView *view in self.tabBarController.view.subviews)
		{
			CGRect _frame = view.frame;
			if(![view isKindOfClass:[UITabBar class]])
			{
				_frame.size.height += 49;
				[view setFrame:_frame];
			}
		}
	}
	else {
		self.tabBarController.tabBar.hidden = NO;
		for(UIView *view in self.tabBarController.view.subviews)
		{
			CGRect _frame = view.frame;
			if(![view isKindOfClass:[UITabBar class]])
			{
				_frame.size.height -= 49;
				[view setFrame:_frame];
			}
		}
	}
}

で、結果はこんな感じに UITabBar がいなくなった所にもちゃんと UITableView が表示されてる。

参考にしたサイトでは UITabBarの Viewを画面の外に動かしていたけど、自分の場合は UIViewControllerの Viewが UITabBarを覆ったみたいで見えなくなったので良しとした。

実際には UIViewController に置いている Viewの位置調整もちょっとやる必要があったのだけど、それはケースバイケースだと思うので割愛。(自分の場合は TableViewと UIViewのサイズを適時変更した。)


ちなみに、このコードは回転してる UIViewController に書いてるけどちゃんと動作してる。特に AppDelegate に入れる必要はなかった。


ところで、UIViewController にくっ付けたUITableView は画面回転時にサイズが自動的に横向きのものに変わらないのかな?
UITableViewController は回転したら勝手にサイズが横向きのものになったと思うのだけど。
IB の View のところで大きくなったり小さくなったりする設定にしてあのだけど、回転後も Portrait時のサイズのまま。
自分が見つけられないだけで、たぶんあると思うんだけどな〜。