2011-01-22 99 views
0

我有一個TabBarController應用程序。tabbar問題中的自定義按鈕

該代碼在didFinishingLaunching方法:

UIImage *buttonImage = [UIImage imageNamed:@"post-button2.png"]; 

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; 

NSLog(@"Button size: %f, %f", buttonImage.size.width, buttonImage.size.height); 

button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height); 
[button setBackgroundImage:buttonImage forState:UIControlStateNormal]; 

CGFloat heightDifference = buttonImage.size.height - self.tabBarController.tabBar.frame.size.height; 

NSLog(@"self.tabBarController.tabBar.frame.size.height: %f", self.tabBarController.tabBar.frame.size.height); 
NSLog(@"heightDifference: %f", heightDifference); 
NSLog(@"%Tabbar: %f, %f", tabBarController.tabBar.center.x, tabBarController.tabBar.center.y); 
if (heightDifference < 0) 
    button.center = tabBarController.tabBar.center; 
else { 
    CGPoint center = self.tabBarController.tabBar.center; 
    center.y = center.y - heightDifference/2.0; 
    button.center = tabBarController.tabBar.center; 
} 

NSLog(@"%Button: %f, %f", button.center.y, button.center.x); 

[tabBarController.view addSubview:button]; 

[self.window addSubview:tabBarController.view]; 
[self.window makeKeyAndVisible]; 

的輸出是:

alt text

問題在哪裏?我可以用硬編碼解決:

CGPoint center = tabBarController.tabBar.center; 
center.x = 160.00; 
center.y = 455.50; 

button.center = center; 

但我不確定這是否正確。

Ty。

回答

0

要設置選項卡的圖像,您需要設置與選項卡關聯的視圖控制器的tabBarItem。

請參閱UITabBarItem參考如何使用自定義圖像創建一個。

因此,您使用自定義圖像創建一個UITabBarItem對象,然後將其設置爲該選項卡的viewcontroller的tabBarItem屬性。

然後,UITabBarController將管理將圖像放在正確的位置。

+0

它不是一個tabbar項目,它是tabbar上的一個自定義按鈕。 – 2011-01-22 20:37:45