2011-01-22 1 views
0

У меня есть приложение TabBarController.Пользовательская кнопка в задаче табуляции

Этот код в методе 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 ref для того, как создать его с пользовательским изображением.

Таким образом, вы создаете объект UITabBarItem с пользовательским изображением, а затем создаете его как свойство tabBarItem контроллера view для вкладки.

После этого UITabBarController будет управлять размещением изображения в нужном месте.

+0

Это не элемент табеля, это настраиваемая кнопка над вкладкой. –