2013-10-07 113 views
0

我一直在这个问题上已经有一段时间了,无法找到解决我的问题。使用故事板与自定义UITabbarController

我有我试图与图像来定制的TabBar视图控制器,我有然而工作我需要使用代码来显示和INIT使用TabBar的视图控制器的自定义图形。我也有一个问题,显示导航栏在我的标签之一的顶部,我认为是连接到我如何启动标签视图控制器

故事板显示应该有一个导航栏在顶部药物选项卡和该视图通过segue连接到标签栏 您可以看到我试图使用storyboard segues将我的视图控制器链接到标签栏控制器。我在MedicationViewController.m

/ 
// MedicationViewController.m 
// fibromapp 
// 
// Created by jamie mcallister on 08/09/2013. 
// Copyright (c) 2013 Jamie McAllister. All rights reserved. 
// 

#import "MedicationViewController.h" 
#import "TakenViewController.h" 
#import "MedsListViewController.h" 
#import "MedsAlarmViewController.h" 

@interface MedicationViewController() 

@end 

@implementation MedicationViewController 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 

     TakenViewController *viewController2 = [[TakenViewController alloc] init]; 
     MedsListViewController *viewController1 = [[MedsListViewController alloc] init]; 
     MedsAlarmViewController *viewController3 = [[MedsAlarmViewController alloc] init]; 


     self.viewControllers = [NSArray arrayWithObjects:viewController1, 
           viewController2, 
           viewController3,nil]; 
     UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:@"Medication" image:[UIImage imageNamed:NULL] tag:1]; 
     UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:@"Taken" image:[UIImage imageNamed:NULL] tag:2]; 
     UITabBarItem *tab3 = [[UITabBarItem alloc] initWithTitle:@"Alarms" image:[UIImage imageNamed:NULL] tag:3]; 
     UIImage* sel = [UIImage imageNamed:@"fmtabSel"]; 


     [viewController1 setTabBarItem:tab1]; 
     [viewController2 setTabBarItem:tab2]; 
     [viewController3 setTabBarItem:tab3]; 

     UIImage* tabBarBackground = [UIImage imageNamed:@"fmtab.png"]; 

     UITabBar *tabBar = self.tabBar; 
     [tabBar setBackgroundImage:tabBarBackground]; 
     [tabBar setSelectionIndicatorImage:sel]; 
    } 
    return self; 
} 


- (void)viewDidLoad 
{ 
UITabBar *tabbar = self.tabBar; 
NSLog(@"%f %f", tabbar.frame.size.width, tabbar.frame.size.height);//used to find the size of the bar 
[super viewDidLoad]; 

UIImage* tabBarBackground = [UIImage imageNamed:@"fmtab.png"]; 
UIImage* sel = [UIImage imageNamed:@"fmtabSel"]; 
    UITabBar *tabBar = self.tabBar; 
    [tabBar setBackgroundImage:tabBarBackground]; 
    [tabBar setSelectionIndicatorImage:sel]; 

    // Do any additional setup after loading the view. 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

@end 

与此代码我得到的标签,但有在该选项卡的顶部没有导航栏下面的代码。

任何人可以建议什么我必须这样做,以解决这个问题?

如果您需要任何更多信息

随便问,我将它编辑成这个问题的底部。 在此先感谢:)

回答

0

如果您希望导航栏位于顶部您应该使用导航控制器填充根控制器(而不仅仅是普通控制器)来填充导航控制器。 水木清华这样的:

TakenViewController *viewController2 = [[TakenViewController alloc] init]; 
MedsListViewController *viewController1 = [[MedsListViewController alloc] init]; 
MedsAlarmViewController *viewController3 = [[MedsAlarmViewController alloc] init]; 
UINavigationController * nc1 = [[UINavigationController alloc] initWithRootViewController:viewController1]; 
UINavigationController * nc2 = [[UINavigationController alloc] initWithRootViewController:viewController2]; 
UINavigationController * nc3 = [[UINavigationController alloc] initWithRootViewController:viewController3]; 

self.viewControllers = [NSArray arrayWithObjects:nc1, 
           nc2, 
           nc3,nil]; 
+0

谢谢你,这代码似乎已经奏效,但它看起来像我的应用程序,现在依靠代码,而不是在一些地区的应用(如导航栏标题)。对我来说有点问题,因为我在故事板的导航栏中有一个按钮,可以将项目添加到列表中 –

+0

正如莱昂纳多说过的,您可以在故事板中执行此操作。我刚刚向你展示了一个移动方向 - 带有导航的tabbar。 – Stas

1

为了有一个导航栏,你必须把使用TabBar控制器和第一之间的UIViewController一个UINavigationController。

所有在故事板来完成,而无需编写一行代码的需求。

+0

那就是如何我已经奠定了我的故事板,但我还没有一个导航栏,当我运行应用程序 –

+0

我的错,我确实现在有一个唠叨吧,它只是没有显示从故事板过这么难查看 :/ –