2014-03-27 3 views
0

У меня есть UITabBar на UIViewController, я поставил UITabBar делегат и это, как я установил UITabBar внутри моего viewDidLoad: методаTabBarItems для UITabBar

// Load UITabBar for FindModels View & call tabBar delegates 
    findModelsTabBar = [[UITabBar alloc] init]; 
    findModelsTabBar.delegate = self; // This sets up tabbardelegate method 

    [findModelsTabBar setTranslucent:NO]; 
    findModelsTabBar.backgroundColor = [UIColor lightGrayColor]; 
    findModelsTabBar.frame = CGRectMake(0.0, screenRectTabBar.size.height - 110, screenRectTabBar.size.width, 45.0); 
    [[UITabBar appearance] setBarTintColor:[UIColor colorWithRed:colorController.lgRed/255.0 green:colorController.lgGreen/255.0 blue:colorController.lgBlue/255.0 alpha:1.0]]; 

    [self.view insertSubview:findModelsTabBar aboveSubview:self.tableView]; // add tabBar to the mainView (appears at the bottom of the screen) 

Тогда у меня есть мой метод делегата, как так

#pragma TabBar delegate 
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 
{ 
    switch (item.tag) { 
     case 0: 
     { 
      NSLog(@"0"); 
     } 
      break; 
     case 1: 
     { 
      NSLog(@"1"); 
     } 
      break; 
     case 2: 
     { 
      NSLog(@"2"); 
      FindModelsViewController *findModelsViewController = [[FindModelsViewController alloc] initWithNibName:@"FindModelsViewController" bundle:nil]; 

      // Sets the back button for the new view that loads (this overrides the usual parentview name with "Back") 
      self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil]; 

      [self.navigationController pushViewController:findModelsViewController animated:YES]; 

      // Set Delegates so you can get the data back 
      [findModelsViewController setDelegate:self]; 
     } 
      break; 
     default: 
      break; 
    } 
} 

Я хотел бы знать, как создать UITabBar позицию с изображения и при щелчке назвал бы TabBar делегатом ...

любая помощь будет оценена по достоинству.

ответ

1

Вы можете легко написать код, как:

UITabBarItem * item0 = [[UITabBarItem alloc] initWithTitle:@"Page 1" 
                image:[UIImage imageNamed:@"page1_image_normal"] 
              selectedImage:[UIImage imageNamed:@"page1_image_selected"]]; 
[item0 setTag:0]; 

UITabBarItem * item1 = [[UITabBarItem alloc] initWithTitle:@"Page 2" 
                image:[UIImage imageNamed:@"page2_image_normal"] 
              selectedImage:[UIImage imageNamed:@"page2_image_selected"]]; 
[item1 setTag:1]; 

UITabBarItem * item2 = [[UITabBarItem alloc] initWithTitle:@"Page 3" 
                image:[UIImage imageNamed:@"page3_image_normal"] 
              selectedImage:[UIImage imageNamed:@"page3_image_selected"]]; 
[item2 setTag:2]; 

[findModelsTabBar setItems:@[item0, item1, item2]]; 
+0

Это отлично сработало! Если я использую только один UITabBarItem, есть ли способ выравнивать его вправо или влево? – HurkNburkS

+0

Насколько я знаю, такого пути нет. Вы можете сделать некоторые трюки, используя 'UITabBarItemPositioning', который определен в SDK iOS 7, но он будет центрировать или заполнить панель вкладок. – ismailgulek

1

Я думаю, что все приведенные ниже ссылки помогут вам, и насколько это касается делегата, когда вы получите, чтобы UITabBarItem просто установил теги для каждого из них, а затем применил ваши условия.

Adding a UITabBar and tabbaritems to the UITabBar through code (NOTE: I don't want to implement TabBarController)

iPhone instantiating a UITabBarItem programmatically

Setting custom UITabBarItem programatically?

How to programmatically add UITabBarItem's Identifier type in iOS?

Tutorial to build a tab bar programmatically

Смежные вопросы