ビューの上とナビゲーション バーのすぐ下にナビゲーション バーがある 1 つのアプリケーションを作成しようとしています タブバーのように見える 4 つのボタンを追加したいのですが、Apple の条件により、タブバー バーを使用していません。見る。上(ナビゲーションバーのすぐ下)で使用したい。そのため、4 つのカスタム ボタンを使用しています。押されたボタンに応じて作成されたビューをプッシュポップするために、ナビゲーションコントローラーを使用しています。タブバーのように実装する方法を説明してください。ハウツー ボタンはタブバー アイテムのように機能します。
4 に答える
UIbuttons
全体を使用したり呼び出したりする代わりに、使用しないUITabbarController
のはなぜですか。コードは次のとおりです。これでうまくいくはずです:)UITabbar
tabbar items
UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(your frame)];
NSMutableArray *tabBarItemsArray= [[NSMutableArray alloc] init];
UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"artist" image:[UIImage imageNamed:@"artist-tab.png"] tag:1];//assigning the title name and image
[tabBarItem1 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:48.0/255.0 green:60.0/255.0 blue:109.0/255.0 alpha:1.0],UITextAttributeTextColor,nil] forState:UIControlStateNormal];//set the color when the tabbar appears
[tabBarItem1 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil] forState:UIControlStateSelected];//sets the color when the tabbar is selected
UITabBarItem *tabBarItem2 = [[UITabBarItem alloc] initWithTitle:@"Facebook" image:[UIImage imageNamed:@"music-tab.png"] tag:2];
[tabBarItem2 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:48.0/255.0 green:60.0/255.0 blue:109.0/255.0 alpha:1.0],UITextAttributeTextColor,nil] forState:UIControlStateNormal];
[tabBarItem2 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil] forState:UIControlStateSelected];
[tabBarItemsArray addObject:tabBarItem1];
[tabBarItemsArray addObject:tabBarItem2];
tabBar.items = tabBarItemsArray;
tabBar.delegate = self;
[self.view addSubview:tabBar];
このメソッドを実装するには、ヘッダーファイルにデリゲートUITabBarDelegateを追加してください。
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSLog(@"Tabbar selected itm %d",item.tag);
// here you can call your view based on tab click you can try using switch statements here based on item.tag
}
これがあなたの質問に答え、役立つことを願っています:)
また、画像を使用してUITabbarをカスタマイズすることもできます...お知らせください。必要に応じて、ここにコードを投稿します。
タブバー コントローラーと同じように動作させるには、同じ手順に従ってプログラムでタブバー コントローラーを作成します。
タブバーを作成
UIButton のクリック時にプッシュする各 UIViewController のインスタンスを作成し、それを rootviewcontroller として追加し、
UINavigationController
そのすべてのオブジェクトをNSArray
その配列に設定してから_tabBarController.viewControllers
、 _tabBarController.view を追加しますself.window
カスタム ビューの作成
次に
UIView
、ビュー内の _tabBarController の位置と同じフレームを使用して を作成し、_tabBarController に表示する を追加UIButton
します。ボタンは背景画像用にカスタマイズしたり、必要な UI の外観に合わせたりすることができます。これらすべてのボタンをサブビューとして追加します。UIView
最後にそのビュー オブジェクトをself.window
サブビューにします。ここまでで、必要なすべてのボタンを備えたカスタム タブバーの外観が得られます。次に、タブバーのように動作させます。
追加した各ボタンにアクションを追加することを忘れないでください。アクションの実装は次のようになります。
- (void)select_tab:(UIButton *)sender
{
_tabBarController.selectedIndex = sender.tag;
//_tabBarController is object of UITabBarController
}
各ボタンにはいくつかのタグ セットが必要であることも忘れないでください。これらのタグは現在のタブバー インデックスのインデックスとして機能するため、ビュー コントローラーを配列に追加するためにタグを指定します。ボタン タグは 0 で始まる必要があります (ゼロ) ベース インデックスから最大ビュー コントローラーまで。
それだけです。これがお役に立てば幸いです。
あなたは怒鳴るポイントのようにすることができます。
- Xib の下部に 4 つの Round Rect Button を配置します。
- ボタンごとに 4 つのクラスを作成し、IBAction とボタンを適切に結合します。次のようなアニメーションなしでプッシュとポップができるようになりました:-
[self.navigationController pushViewController:ObjYourViewcontroler animated:NO];
そしてそれを次のようにポップします:-
[self.navigationController popViewControllerAnimated:NO];
完全なコードで確認してください。タブバーコントローラーのように機能します。
localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:0];
AllRumsController *allRumsScreen=[[AllRumsController alloc]init];
RumRecipesController *rumRecipesScreen =[[RumRecipesController alloc] init];
PartyPlannerClass *aPartyPlannerScreen =[[PartyPlannerClass alloc] init];
BarTendingClass *aBarTendingScreen =[[BarTendingClass alloc] init];
MoreControllersClass *moreControllerScreen =[[MoreControllersClass alloc] init];
controllersArray = [[NSArray alloc]initWithObjects:allRumsScreen,rumRecipesScreen,aPartyPlannerScreen,aBarTendingScreen,moreControllerScreen,nil];
for(int i=0;i<[controllersArray count];i++)
{
UINavigationController *localNavigationController=[[UINavigationController alloc]initWithRootViewController:[controllersArray objectAtIndex:i]];
localNavigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
if(!i)
[localNavigationController setTitle:@"All Rums"];
else if(i ==1)
[localNavigationController setTitle:@"Recipes"];
else if(i ==2)
[localNavigationController setTitle:@"PartyPlanner"];
else if(i ==3)
[localNavigationController setTitle:@"BarTending"];
else
[localNavigationController setTitle:@"More"];
localNavigationController.navigationBar.hidden = YES;
[localViewControllersArray addObject:localNavigationController];
[localNavigationController release];
}
[allRumsScreen release];
[rumRecipesScreen release];
[aPartyPlannerScreen release];
[aBarTendingScreen release];
[moreControllerScreen release];
if(contentView == nil)
{
contentView = [[UIView alloc] initWithFrame:CGRectMake(0,0,MAX_WIDTH, 431)];
contentView.backgroundColor = [UIColor clearColor];
}
if(bottomView == nil)
{
bottomView = [[UIView alloc] initWithFrame:CGRectMake(0,431,MAX_WIDTH, 49)];
bottomView.backgroundColor = [UIColor clearColor];
}
allButtonsArray = [[NSMutableArray alloc]initWithCapacity:0];
for(int i=0;i<[localViewControllersArray count];i++)
{
UIButton *aButton=[UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame=CGRectMake(i*64,0,64,49);
[aButton setTag:i];
if(!i)
{
[aButton setImage:[UIImage imageNamed:@"ourrums_n.png"] forState:UIControlStateNormal];
[aButton setImage:[UIImage imageNamed:@"ourrums_a.png"] forState:UIControlStateHighlighted];
[aButton setImage:[UIImage imageNamed:@"ourrums_n.png"] forState:UIControlStateDisabled];
}
else if(i ==1)
{
[aButton setImage:[UIImage imageNamed:@"Recipies_n.png"] forState:UIControlStateNormal];
[aButton setImage:[UIImage imageNamed:@"Recipies_a.png"] forState:UIControlStateHighlighted];
[aButton setImage:[UIImage imageNamed:@"Recipies_n.png"] forState:UIControlStateDisabled];
}else if(i ==2)
{
[aButton setImage:[UIImage imageNamed:@"myevents_n.png"] forState:UIControlStateNormal];
[aButton setImage:[UIImage imageNamed:@"myevents_a.png"] forState:UIControlStateHighlighted];
[aButton setImage:[UIImage imageNamed:@"myevents_n.png"] forState:UIControlStateDisabled];
}else if(i ==3)
{
[aButton setImage:[UIImage imageNamed:@"bartending_n.png"] forState:UIControlStateNormal];
[aButton setImage:[UIImage imageNamed:@"bartending_a.png"] forState:UIControlStateHighlighted];
[aButton setImage:[UIImage imageNamed:@"bartending_n.png"] forState:UIControlStateDisabled];
}else
{
[aButton setImage:[UIImage imageNamed:@"more_n.png"] forState:UIControlStateNormal];
[aButton setImage:[UIImage imageNamed:@"more_a.png"] forState:UIControlStateHighlighted];
[aButton setImage:[UIImage imageNamed:@"more_n.png"] forState:UIControlStateDisabled];
}
[aButton addTarget:self action:@selector(showViewOfButton:) forControlEvents:UIControlEventTouchUpInside];
[allButtonsArray addObject:aButton];
[bottomView addSubview:aButton];
}
[[[[UIApplication sharedApplication] delegate] window] addSubview:contentView];
[[[[UIApplication sharedApplication] delegate] window] addSubview:bottomView];
[bottomView setHidden:YES];
[contentView setHidden:YES];
[self showViewOfButton:[allButtonsArray objectAtIndex:0]];
-(void)showViewOfButton:(id)sender
{
if(start == YES)
{
start = NO;
[repeat setTag:[sender tag]];
}
else if([repeat tag] == [sender tag])
{
return;
}
else
{
[repeat setTag:[sender tag]];
}
if(previous!=nil)
{
[previous setImage:[previous imageForState:UIControlStateDisabled] forState:UIControlStateNormal];
}
if( [[contentView subviews] count] )
{
if([[RumsDataHolder sharedHolder] mailController])
{
[[[[RumsDataHolder sharedHolder] mailController] retain] dismissModalViewControllerAnimated:NO];
}
[[[contentView subviews] objectAtIndex:0] removeFromSuperview];
}
if( ((UIView *)sender).tag == 0)
{
[sender setImage:[UIImage imageNamed:@"ourrums_a.png"] forState:UIControlStateNormal];
[contentView addSubview:[[localViewControllersArray objectAtIndex:0] view]];
}
else if( ((UIView *)sender).tag == 1)
{
[sender setImage:[UIImage imageNamed:@"Recipies_a.png"] forState:UIControlStateNormal];
[contentView addSubview:[[localViewControllersArray objectAtIndex:1] view]];
}
else if(((UIView *)sender).tag == 2)
{
[sender setImage:[UIImage imageNamed:@"myevents_a.png"] forState:UIControlStateNormal];
[contentView addSubview:[[localViewControllersArray objectAtIndex:2] view]];
}
else if( ((UIView *)sender).tag == 3)
{
[sender setImage:[UIImage imageNamed:@"bartending_a.png"] forState:UIControlStateNormal];
[contentView addSubview:[[localViewControllersArray objectAtIndex:3] view]];
}
else if(((UIView *)sender).tag == 4)
{
[sender setImage:[UIImage imageNamed:@"more_a.png"] forState:UIControlStateNormal];
[contentView addSubview:[[localViewControllersArray objectAtIndex:4] view]];
}
else
;
previous=sender;
}