如何实现4个圆形矩形按钮,其行为类似于tabbars项?

时间:2013-02-26 05:54:44

标签: iphone ios objective-c ios5 ios6

我正在尝试创建一个在视图顶部有导航栏的应用程序,在导航栏的下方我想要添加4个看起来像tabbar的按钮,我没有使用tabbar bar,因为Apple CONDITIONS。因为Apple推荐tabbar只在视图的底部。我想在顶部使用它(在导航栏的下方)。所以我使用4个自定义按钮。我正在使用导航控制器按下弹出视图,这些视图是根据按下的按钮创建的。你可以解释如何像tabbar一样实现。如何按钮将像tabbar项目一样工作。

4 个答案:

答案 0 :(得分:0)

你可以像吼叫一样。

  • 在Xib底部放置4个圆形矩形按钮。
  • 为每个按钮创建4个类,并使用按钮提升IBAction。 现在你可以在没有动画的情况下推送和弹出: -

[self.navigationController pushViewController:ObjYourViewcontroler animated:NO];

然后弹出: -

[self.navigationController popViewControllerAnimated:NO];

答案 1 :(得分:0)

只需查看完整代码即可。就像tabbar controller一样。

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;
}

答案 2 :(得分:0)

好吧,而不是使用UIbuttons或调用整个UITabbarController为什么不使用UITabbartabbar 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 ...让我知道..如果你愿意,我会在这里发布代码。

答案 3 :(得分:0)

要像tabbar控制器一样运行,请按照相同的步骤以编程方式创建tabbar控制器,

创建标签栏

  

为每个想要推动单击UIButton的UIViewController创建实例,将其作为UINavigationController的rootviewcontroller及其所有对象添加到NSArray并将该数组设置为_tabBarController.viewControllers,然后将_tabBarController.view添加到self.window

创建自定义视图

  

接下来创建一个UIView,其框架与视图中_tabBarController的位置相同,添加要在_tabBarController上显示的UIButton,按钮可以自定义任何背景图像或者匹配您的需求UI外观,将所有这些按钮作为子视图添加到UIView,最后将该对象添加到self.window子视图。在此之前,您将看到自定义标签栏以及您想要的所有按钮,现在使其表现得像tabbar。

不要忘记为你添加的每个按钮添加动作,动作实现类似,

- (void)select_tab:(UIButton *)sender
{
   _tabBarController.selectedIndex = sender.tag;
   //_tabBarController is object of UITabBarController
}

请记住,它还需要每个按钮都应该有一些标记集,这些标记将作为当前tabbar索引的索引,所以给你标记以便将viewcontrollers添加到数组中,按钮标记应该开始使用0(零)基本索引,直至最大的视图控制器。

就是这样,希望这有帮助!