LoginSignup
2
2

More than 5 years have passed since last update.

UITabBarControllerを用いて、メニュー処理の実現方法

Last updated at Posted at 2015-05-25

iOSでは、UITabBarControllerを用いれば、TAB処理を簡単に実現できます。
ただ、TABを用いるのでなく、独自のメニューのUIにてViewの切り替えを行いたい場合などがあると思います。
その場合は、自前でUITabBarControllerのようなものを実装する必要があるかと思いますが、下記手順にてUITabBarControllerの仕組みを利用して、シンプルに実現可能です。

1.通常通り、UITabBarControllerにて画面遷移を実装する。

2.各TabのFirstViewプロパティの「Hide Bottom Bar on Push」をチェックする。
 →TabBarが非表示となります。

3.各Viewにてメニューの表示処理を実装します。
 →NavigationBarなどにModal表示で実装します。

4.メニューの各ボタンが押された場合の処理を実装します。

下記の例では、Modalから呼び出されたViewControllerを取得し(UITabController)、selectedIndexを変更し、ModalViewを閉じています。


- (void)menu2:(id)sender
{
    // 親ViewController(UITabBarController)を取得する
    UITabBarController* parentViewController = (UITabBarController*)[self presentingViewController];
    // TABを変更する
    parentViewController.selectedIndex = 1;
    // メニューを閉じる
    [self dismissViewControllerAnimated:YES completion:nil];
}

上記でUITabbarControllerを用いずに、Tabのような動きが可能になります。

サンプルはgithubからどうぞ
menusample

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