2011-09-01 97 views
1

第一次海報。提前致謝。爲什麼不顯示UITabBarItems的標題?

我想在我的iPhone應用程序中使用UITabBar。我被引導認爲我不能使用UITabViewController,因爲我將它嵌套在UINavigationController中。我讀過你可以做到這一點,但你必須使用UITabBar而不是完整的控制器。我可以使用Interface Builder進行這項工作,但是我有很多冗餘的XIB,我寧願在代碼中使用它。我想知道魔法是如何運作的。

這裏是我的控制器的.m:

#import "DumbViewController.h" 

@implementation DumbViewController 

-(void) loadView { 
    UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 150, 30)]; 
    [label setText:@"hello world"]; 
    [view addSubview:label]; 
    [label release]; 

    UITabBar *tb = [[UITabBar alloc] initWithFrame:CGRectMake(0, [view frame].size.height - 64, [view frame].size.width, 64)]; 
    [tb setDelegate:self]; 
    [tb setItems:[NSArray arrayWithObject:[[[UITabBarItem alloc] initWithTitle:@"bob" image:[UIImage imageNamed:@"21-skull.png"] tag:0] autorelease]]]; 
    [view addSubview:tb]; 
    [tb release]; 

    [self setView:view]; 
    [view release]; 
} 

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item { 
    NSLog(@"selected item %@", item); 
} 

@end 

當我運行應用程序,讓我的標籤,並在底部的標籤欄。我得到了愚蠢的小頭骨圖標,但「鮑勃」從不顯示。按照你所期望的那樣點擊頭骨。

爲什麼不顯示該標題?

謝謝!

回答

0

的問題是,我是設置UITabBar的幀中的父視圖的框外。

我在UITabBar分配行之後添加了這一行。

[tb setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin]; 

這裏的優點是,即使我旋轉或如果應用程序在iPad上運行,選項卡欄仍停留在屏幕的底部。

我也從64像素高度更改爲44像素高度,因爲它看起來更像是XIB最初的樣子。

0

嘗試下面的代碼....

UITabBarItem *someTabBarItem = [[UITabBarItem alloc] initWithTitle:@"bob" image:[UIImage imageNamed:@"21-skull.png"] tag:0]; 
    NSArray *tabBarItems = [[NSArray alloc] initWithObjects:someTabBarItem,nil]; 
    [tabBar setItems:tabBarItems animated:NO]; 
    [tabBar setSelectedItem:someTabBarItem]; 
    [tabBarItems release]; 
    [someTabBarItem release]; 
+0

這產生了與我原來的代碼相同的結果。無題。 – bmauter

+0

我建議你去用UIButtons而不是TabbarItems。 –

+0

爲什麼?我的標籤欄只是在屏幕外。現在它在屏幕上,它完美的工作。 UIButton有什麼優勢? – bmauter