UITabBarControllerの学習
- YouTube に飛びます。
- Note.com に飛びます
POINT 1
UITabBarControllerDelegate の設定
@interface BooksXCOTabBarFirstViewController () <UITabBarControllerDelegate>
POINT 2
UITabBarControllerDelegate の設定
self.tabBarController.delegate = self;
POINT 3
タブバーと同じ動きになるように設定
NSInteger selectedIndex = button.tag;
UIViewController *vc = self.tabBarController.viewControllers[selectedIndex];
[self.tabBarController setSelectedViewController:vc];
self.tabBarController.title = vc.title;
POINT 4
ボタンのタグとタブの位置情報を紐付ける
tabButton.tag = index;
POINT 5
タイトルを選択されたビューのタイトにる切り替える
self.tabBarController.title = viewController.title;
サンプルコード
/// @property (retain,nonatomic) NSArray *viewControllerNameList;
- (IBAction)callTabBarController:(id)sender {
// タブを要素に持つ tabVCListを初期化
NSMutableArray *tabVCList = [NSMutableArray new];
for (NSString *viewControllerName in viewControllerNameList) {
if (viewControllerName) {
/// Tabに設定するViewControllerのインスタンスを生成.
Class class = NSClassFromString(viewControllerName);
vc = [[class alloc] init];
[tabVCList addObject:vc];
}
}
// UITabControllerの作成する.
UITabBarController *tbc = [[UITabBarController alloc] init];
// ViewControllerを設定する.
[tbc setViewControllers:tabVCList];
tbc.selectedViewController = vc;
UINavigationController *nv = [[UINavigationController alloc] initWithRootViewController:tbc];
[self presentViewController:nv animated:YES completion:nil];
}
#import <UIKit/UIKit.h>
@interface BooksXCOTabBarFirstViewController : UIViewController
@end
#import "BooksXCOTabBarFirstViewController.h"
/// POINT 1:UITabBarControllerDelegate の設定
@interface BooksXCOTabBarFirstViewController () <UITabBarControllerDelegate>
@end
@implementation BooksXCOTabBarFirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Viewの背景色をCyanに設定する.
self.view.backgroundColor = UIColor.cyanColor;
//tabBarItemのアイコンをFeaturedに、タグを1と定義する.
self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:1];
/// POINT 2:UITabBarControllerDelegate の設定
self.tabBarController.delegate = self;
// Controllerのタイトルを設定する.
self.title = @"My 1st View";
self.tabBarController.title = self.title;
// Viewの背景色をCyanに設定する.
self.view.backgroundColor = UIColor.cyanColor;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// 表示前に設定
[self setupTapTabButtons];
}
/*
ボタンイベント
*/
- (IBAction)onClickTabButton:(id)sender {
UIButton *button = sender;
/// POINT 3:タブバーと同じ動きになるように設定
NSInteger selectedIndex = button.tag;
UIViewController *vc = self.tabBarController.viewControllers[selectedIndex];
[self.tabBarController setSelectedViewController:vc];
self.tabBarController.title = vc.title;
}
- (void)setupTapTabButtons {
CGFloat TAB_BUTTON_WIDHT = 150;
CGFloat TAB_BUTTON_HEIGHT = 50;
CGFloat TAB_BUTTON_POSITION_Y = 300;
NSInteger index = 0;
NSInteger max = self.tabBarController.viewControllers.count;
for (UIViewController *vc in self.tabBarController.viewControllers) {
CGPoint point = CGPointMake((self.view.frame.size.width / max) * index + ((self.view.frame.size.width/ max) - TAB_BUTTON_WIDHT )/ 2, TAB_BUTTON_POSITION_Y);
// ボタンの定義をおこなう.
UIButton *tabButton = [[UIButton alloc] initWithFrame:CGRectMake(point.x,point.y,TAB_BUTTON_WIDHT,TAB_BUTTON_HEIGHT)];
/// POINT 4:ボタンのタグとタブの位置情報を紐付ける
tabButton.tag = index;
tabButton.backgroundColor = UIColor.orangeColor;
tabButton.layer.masksToBounds = true;
[tabButton setTitle:vc.title forState:UIControlStateNormal];
tabButton.layer.cornerRadius = 20.0;
[tabButton addTarget:self action:@selector(onClickTabButton:) forControlEvents:UIControlEventTouchUpInside];
// ボタンをViewに追加する.
[self.view addSubview:tabButton];
index++;
}
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
/// POINT 5:タイトルを選択されたビューのタイトにる切り替える
self.tabBarController.title = viewController.title;
}
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers {
}
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed {
}
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed {
}
- (UIInterfaceOrientationMask)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController {
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)tabBarControllerPreferredInterfaceOrientationForPresentation:(UITabBarController *)tabBarController {
return UIInterfaceOrientationPortrait;
}
//- (nullable id <UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController
// interactionControllerForAnimationController: (id <UIViewControllerAnimatedTransitioning>)animationController {
//
//}
//- (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
// animationControllerForTransitionFromViewController:(UIViewController *)fromVC
// toViewController:(UIViewController *)toVC {
//
//}
@end
#import <UIKit/UIKit.h>
@interface BooksXCOTabBarSecondViewController : UIViewController
@end
#import "BooksXCOTabBarSecondViewController.h"
@interface BooksXCOTabBarSecondViewController () <UITabBarControllerDelegate>
@end
@implementation BooksXCOTabBarSecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Viewの背景色をGreenに設定する.
self.view.backgroundColor = UIColor.greenColor;
// tabBarItemのアイコンをFeaturedに、タグを2と定義する.
self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:2];
self.tabBarController.delegate = self;
self.title = @"My 2nd View";
// Viewの背景色を定義する.
self.view.backgroundColor = UIColor.blueColor;
// ボタンの定義をおこなう.
UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,100,50)];
closeButton.backgroundColor = [UIColor.yellowColor colorWithAlphaComponent:0.8];
closeButton.layer.masksToBounds = true;
[closeButton setTitleColor:UIColor.redColor forState:UIControlStateNormal];
closeButton.titleLabel.font = [UIFont systemFontOfSize:20];
[closeButton setTitle:@"CLOSE" forState:UIControlStateNormal];
closeButton.layer.cornerRadius = 20.0;
closeButton.layer.position = CGPointMake(self.view.bounds.size.width/2,200);
closeButton.layer.borderColor = UIColor.redColor.CGColor;
closeButton.layer.borderWidth = 2.0;
[closeButton addTarget:self action:@selector(onClickCloseButton:) forControlEvents:UIControlEventTouchUpInside];
// ボタンをViewに追加する.
[self.view addSubview:closeButton];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// 表示前に設定
[self setupTapTabButtons];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (void)onClickCloseButton:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
/*
ボタンイベント
*/
- (IBAction)onClickTabButton:(id)sender {
UIButton *button = sender;
NSInteger selectedIndex = button.tag;
UIViewController *vc = self.tabBarController.viewControllers[selectedIndex];
[self.tabBarController setSelectedViewController:vc];
self.tabBarController.title = vc.title;
}
- (void)setupTapTabButtons {
CGFloat TAB_BUTTON_WIDHT = 150;
CGFloat TAB_BUTTON_HEIGHT = 50;
CGFloat TAB_BUTTON_POSITION_Y = 300;
NSInteger index = 0;
NSInteger max = self.tabBarController.viewControllers.count;
for (UIViewController *vc in self.tabBarController.viewControllers) {
CGPoint point = CGPointMake((self.view.frame.size.width / max) * index + ((self.view.frame.size.width/ max) - TAB_BUTTON_WIDHT )/ 2, TAB_BUTTON_POSITION_Y);
// ボタンの定義をおこなう.
UIButton *tabButton = [[UIButton alloc] initWithFrame:CGRectMake(point.x,point.y,TAB_BUTTON_WIDHT,TAB_BUTTON_HEIGHT)];
tabButton.tag = index;
tabButton.backgroundColor = UIColor.orangeColor;
tabButton.layer.masksToBounds = true;
[tabButton setTitle:vc.title forState:UIControlStateNormal];
tabButton.layer.cornerRadius = 20.0;
[tabButton addTarget:self action:@selector(onClickTabButton:) forControlEvents:UIControlEventTouchUpInside];
// ボタンをViewに追加する.
[self.view addSubview:tabButton];
index++;
}
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
self.tabBarController.title = viewController.title;
}
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers {
}
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed {
}
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed {
}
- (UIInterfaceOrientationMask)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController {
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)tabBarControllerPreferredInterfaceOrientationForPresentation:(UITabBarController *)tabBarController {
return UIInterfaceOrientationPortrait;
}
//- (nullable id <UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController
// interactionControllerForAnimationController: (id <UIViewControllerAnimatedTransitioning>)animationController {
//
//}
//- (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
// animationControllerForTransitionFromViewController:(UIViewController *)fromVC
// toViewController:(UIViewController *)toVC {
//
//}
@end
関連記事
【About】(http://qiita.com/sunstripe) - サンストライプ
制作チーム:サンストライプ
(月1 WEBコンテンツをリリースして便利な世の中を作っていくぞ!!ボランティアプログラマー/デザイナー/イラストレーター/その他クリエイター声優募集中!!)
緩募
地域情報 THEメディア
THE メディア 地域活性化をテーマに様々なリリース情報も含め、記事をお届けしてます!!
https://the.themedia.jp/
ゼロからはじめる演劇ワークショップ
多様化の時代に向けて他者理解を鍛える
プログラミングワークショップ・ウェブ塾の開講!!!
様々なテーマでプログラミングに囚われずに取り組んでいきます。
詳しくはこちら ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
プログラミングサロン 月1だけのプログラミング学習塾
協力応援 / 支援者の集い
チーム:サンストライプ
プログラミングラボ
一緒にポートフォリオを作りませんか?現場の体験やそれぞれの立場から年齢関係なく作品を作りたい方々と一緒にチームを作って、作品を作っています。現場に行きたい人には、職場紹介や職場の体験や悩み相談なども受けております。
様々な職種からプログラミングの知識を得たい、デザインの知識を得たい、データーベースの知識を得たいという人が集まっております。
週1のミーティングにそれぞれの近況と作業報告して、たまにリモート飲み会などをしております!!
興味がある方は、DMに話しかけてみてください。
トラストヒューマン
http://trusthuman.co.jp/
私たちは何よりも信頼、人と考えてます。
「コンサルティング」と「クリエイティブ」の両角度から「人材戦略パートナー」としてトータル的にサポートします!!
キャリア教育事業
青空プログラミング
広域学習支援プラットフォーム『のびのび日和』
#スポンサー募集
ネリム
https://nerim.co.jp/
配信事業などを映像コンテンツなどの制作しております