![](/img/trans.png)
[英]How do I set custom navigation bar(title + titleView) for all navigation controllers and view controller?
[英]How to set title of Navigation Bar from a View Controller of a subview?
我有一個視圖控制器,在啟動時有導航欄。
在一些UIViewConrollers之后,我添加了一個tabbar控制器,我為每個標簽欄創建了四個標簽欄和四個視圖控制器。
現在如果我創建帶有與每個選項卡相關的導航欄的標簽欄,它會在頂部顯示兩個導航欄,如果創建沒有導航欄的標簽欄,它將只顯示一個標簽欄,但我無法將與每個標簽視圖控制器對應的標題添加到該導航酒吧
這是我創建標簽欄的代碼
EventDetailViewController *firstViewController = [[EventDetailViewController alloc]init];
firstViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"Home" image:[UIImage imageNamed:@"Multimedia-Icon-Off.png"] tag:2]autorelease]; MoreInfo *secondViewController = [[MoreInfo alloc]init];
secondViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"Info" image:[UIImage imageNamed:@"Review-Icon-Off.png"] tag:0]autorelease];
PlaceInfoViewController *thirdViewController = [[PlaceInfoViewController alloc]init];
thirdViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"Lugar" image:[UIImage imageNamed:@"Place-icon-OFF.png"] tag:1]autorelease];
FavoritesViewController *forthViewController = [[FavoritesViewController alloc]init];
forthViewController.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"Compartir" image:[UIImage imageNamed:@"Compartir-Icon-Off.png"] tag:3]autorelease];
UITabBarController *tabBarController = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tabBarController.viewControllers = [[NSArray alloc] initWithObjects:firstViewController, secondViewController, thirdViewController, forthViewController, nil];
tabBarController.view.frame = self.view.frame;
[self.view addSubview:tabBarController.view];
[firstViewController release];
[secondViewController release];
[thirdViewController release];
[forthViewController release];
您可以獲取對視圖控制器的navigationItem
的引用並設置其title
。
[[aViewController navigationItem] setTitle:@"My Title"];
請注意,如果您還沒有將所有視圖控制器嵌入到導航控制器中,則需要將它們嵌入到導航控制器中。 這樣他們實際上會得到帶導航項的導航欄,你不必自己繪制任何一個。
NSMutableArray *tabs = [NSMutableArray array];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[tabs addObject:nav];
nav = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[tabs addObject:nav];
...
[tabBarController setViewControllers:tabs];
編輯:您的標簽欄控制器位於導航控制器內,因此您需要首先獲得對標簽欄控制器的引用。 在視圖控制器的viewDidLoad
方法中嘗試[[[self tabBarController] navigationItem] setTitle:...]
。
請轉到EventDetailViewController.m並定義以下方法。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.title = NSLocalizedString(@"Catalogues", @"Catalogues");
//self.tabBarItem.image = [UIImage imageNamed:@"book"];
}
return self;
}
希望它能在每個viewController.m中工作,定義上面的初始化程序。
您無法為tabbar控件設置標題。您必須在每個viewcontrollers上使用UINavigationBar
對於UINavigationBar:將導航欄拖動到xib / nib,然后添加標題或以編程方式執行
我自己通過將父項的導航項的引用傳遞給子視圖控制器來獲得解決方案。大家幫忙... :)
你不能設置導航欄的標題。 在導航中設置視圖或子視圖的標題。 self.title = @ “歡迎”; 只需設置標題即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.