2015-06-19 50 views
0

我創建了一個標籤欄控制器並在其中嵌入了多個視圖控制器。然後我嘗試在該視圖的相應swift文件中設置特定的選項卡欄項目。問題是我通過重寫「viewDidLoad」函數來更改項目。所以只有在用戶觸摸該項目後纔會更新。使用swift更改故事板標籤欄項目的更好方法是什麼?使用swift更改故事板標籤欄圖標

+0

我得到了它使用下面的答案 http://stackoverflow.com/a/25868869/3498764 – bertabus

回答

1
  1. 創建的UITabBarController的子類
  2. 集這個自定義類的TabBarController的
  3. 現在你覆蓋的UITabBarController viewDidLoad方法
  4. 那裏你可以訪問所有的TabItems和之前改變他們的文字/圖片 ViewController被加載。

    class CustomTabBarController: UITabBarController { 
    
    override func viewDidLoad() { 
        super.viewDidLoad() 
    
        let allItems:[AnyObject] = self.tabBar.items! 
        var item1:UITabBarItem = allItems[0] as! UITabBarItem 
        var item2:UITabBarItem = allItems[1] as! UITabBarItem 
    
    
        item1.image = UIImage(named: "[email protected]")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) 
        item2.image = UIImage(named: "[email protected]")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) 
    } 
    

    }

在自定義類

的代碼應該像如上...... 可以設置selectedState圖片以及....

這裏是結果..

enter image description here

相關問題