I.概要
UINavigationControllerと同様に、UITabBarControllerも複数のコントローラーを簡単に管理し、コントローラーを簡単に切り替えることができます。典型的な例は、QQやWeChatなどのアプリケーションです。
2つのUITabBarControllerはシンプルで実用的です
2.1UITabBarControllerを使用する手順
-
UITabBarControllerを初期化します
-
UIWindowのrootViewControllerをUITabBarControllerに設定します
-
特定の状況に応じて、addChildViewControllerメソッドを使用して対応する数の子コントローラーを追加します
2.2 UITabBar
-
UITabBarControllerにN個のサブコントローラーがある場合、UITabBar内のサブコントロールとしてN個のUITabBarButtonがあります。
-
UITabBarControllerに4つのサブコントローラーがある場合、UITabBarの構造はおおよそ次のようになります。
2.3 UITabBarButton
-
UITabBarButtonに表示されるコンテンツは、対応するサブコントローラーのtabBarItemプロパティによって決まります。
-
UITabBarItemには、UITabBarButtonのコンテンツに影響を与える次のプロパティがあります。
- タイトルテキスト:@property(nonatomic、copy)NSString * title;
- 画像:@property(nonatomic、retain)UIImage * image;
- 選択時のアイコン:@property(nonatomic、retain)UIImage * selectedImage;
- リマインダー番号:@property(nonatomic、copy)NSString * badgeValue;
2.4UITabBarControllerを初期化する場所
初期化
- AppDelegate.mのdidFinishLaunchingWithOptionsメソッドは以前に初期化されました
- Xcode11の後、SceneDelegate.mで初期化します
理由
- Xcode 11の新しいプロジェクトは、デフォルトでUISceneを介して複数のUIWindowを管理するアプリケーションを作成します。AppDelegateに加えて、プロジェクトにはSceneDelegateもあります。これは、iPadOSが複数のウィンドウをサポートする結果を達成するためです。
- AppDelegate.hにはウィンドウプロパティがなくなり、ウィンドウプロパティはSceneDelegate.hで定義され、AppDelegateにはシーンの新しいプロキシメソッドがあり、SceneDelegateにも対応するプロキシメソッドがあります。
3つのコード(SceneDelegate.m—> willConnectToSessionメソッド)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
UITabBarController * tb = [[UITabBarController alloc] init]; //設定控制器是ウィンドウ的根控制自己.window.rootViewController = tb; // b。创建子控 制器UIViewController * c1 = [[UIViewController alloc] init]; c1.view.backgroundColor = [UIColor GrayColor]; c1.view.backgroundColor = [UIColor greenColor]; c1.tabBarItem.title = @ "消息"; c1.tabBarItem.image = [UIImage imageNamed:@ "tab_recent_nor"]; c1.tabBarItem.selectedImage = [UIImage imageNamed:@ "tab_recent_select"]; c1.tabBarItem.badgeValue = @ "123"; UIViewController * c2 = [[UIViewController alloc] init]; c2.view.backgroundColor = [UIColor brownColor]; c2.tabBarItem.title = @ "連絡先"; c2.tabBarItem.image = [UIImage imageNamed:@ "tab_buddy_nor"]; c2.tabBarItem.selectedImage = [UIImage imageNamed:@ "tab_buddy_select"]; UIViewController * c3 = [[UIViewController alloc] init]; c3.view.backgroundColor = [UIColor greenColor]; c3.tabBarItem.title = @ "PIN态"; c3.tabBarItem.image = [UIImage imageNamed:@ "tab_qworld_nor"]; c3.tabBarItem.selectedImage = [UIImage imageNamed:@ "tab_qworld_select"]; UIViewController * c4 = [[UIViewController alloc] init]; c4.view.backgroundColor = [UIColor blueColor]; c4.tabBarItem.title = @ "開始置"; c4.tabBarItem.image = [UIImage imageNamed:@ "tab_me_nor"]; c4.tabBarItem。 // c。子コントローラーをITabBarControllerに追加します //c.1最初の方法 // [tb addChildViewController:c1]; // c.22番目の方法 tb.viewControllers = @ [c1、c2、c3、 c4]; |