2017-09-04 58 views
0

我需要中間標籤欄項目看起來不同於其他人。這裏有一個例子:如何自定義單個標籤欄項目的外觀?

enter image description here

我怎樣才能做到這一點?

編輯: 除了中間的所有標籤以標準方式對選擇作出反應。選中時只有中間選項卡看起來相同。

+0

我有答案,但在目標C。你可以設置該選項卡的setSelectionIndicatorImage。您必須創建圖像並將其設置爲setSelectionIndicatorImage選定的選項卡,而對於其他選項卡則爲零或您想要設置 –

+0

如果可以,請將其作爲答案發布在objective-c中。如果它能正常工作,我會試着標記爲正確的。謝謝你的評論 – user3400881

+0

好吧,我會嘗試將它轉換成Swift –

回答

0

您應該子類UITabBarController並更改內的items陣列中第三個UITabBarItem的外觀。

class CustomTabBarController: UITabBarController { 
    override func awakeFromNib() { 
     super.awakeFromNib() 

     guard let items = tabBar.items else { 
      return 
     } 

     for i in 0 ..< items.count { 
      item.image = image 
      item.selectedImage = selectedImage 

      [...] 
     } 
    } 
} 
1
Take a subclass of TabbarController.Remember to call `<UITabBarControllerDelegate>` 


    func viewDidAppear(_ animated: Bool) { 
     super.viewDidAppear(animated) 
     // 
     tabBar.selectionIndicatorImage = returnImageofSelectetab() 
    } 

//Method called everytime when you select tab. 

    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) { 

     if tabBarController.tabBar.selectionIndicatorImage == nil { 
      print("Starting point delegate: Selection indicator image is nill") 
     } 
     else { 
      print("Starting Point Of delegate: Selection indicator image is available") 
     } 

//HERE i gave index where I want to set image. 
     if tabBarController.selectedIndex == 2 { 
      tabBarController.tabBar.selectionIndicatorImage = nil 
     } 
     else { 
      tabBarController.tabBar.selectionIndicatorImage = returnImageofSelectetab() 
     } 
     if tabBarController.tabBar.selectionIndicatorImage == nil { 
      print("Ending point delegate: Selection indicator image is nill") 
     } 
     else { 
      print("Ending Point Of delegate: Selection indicator image is available") 
     } 
    } 

     func returnImageofSelectetab() -> UIImage { 
     //HERE 'img_sel' is YOUR SELECTED IMAGE SET AS BACKGROUND OF YOUR TABBARITEM 
      let selTab = UIImage(named: "img_sel")?.withRenderingMode(.alwaysOriginal) 
      let tabSize = CGSize(width: view.frame.width/5, height: 49) 
      UIGraphicsBeginImageContext(tabSize) 
      selTab?.draw(in: CGRect(x: 0, y: 0, width: tabSize.width, height: tabSize.height)) 
      let reSizeImage: UIImage? = UIGraphicsGetImageFromCurrentImageContext() 
      UIGraphicsEndImageContext() 
      return reSizeImage! 
     } 
相關問題