我一直在研究这个问题已有一段时间了,无法找到问题的解决方案。
我有一个tabbar视图控制器,我试图自定义图像,我有自定义图形工作,但我需要使用代码显示和初始化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 :(得分:1)
要拥有导航栏,您必须在tabbar控制器和第一个UIViewController之间放置一个UINavigationController。
所有这些都可以在故事板中完成而无需编写一行代码。
答案 1 :(得分:0)
如果您希望导航栏位于顶部您应该使用根控制器(而不仅仅是普通控制器)填充您的tabbar控制器。 像这样的Smth:
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];