2015-04-04 114 views
-1
override func viewDidLoad() { 
    super.viewDidLoad() 
    tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0) 
} 

我把它放在我的選項卡的視圖控制器之一。但是,這隻能設置具體的視圖。我複製並粘貼到所有其他控制器,但只有當我點擊它們時纔會設置。 (如果我不點擊它們,viewDidLoad不會運行。)如何更改選項卡欄控制器中每個選項卡的外觀?

我希望所有選項卡在顯示所有選項卡時立即顯示此圖像。

回答

0

一種選擇是循環瀏覽標籤欄的所有控制器。在你的UITabBarController初始化(和它的viewControllers被填充)或你的第一個視圖控制器的viewDidLoad(它只需要運行一次)之後放置這個代碼。

// Loop through ALL the controllers in the TabBarController and set their tab bar item imageInsets. 
for (index, controller) in enumerate(tabBarController.viewControllers) { 
    controller.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0) 
} 
相關問題