2017-01-23 40 views
0

我programicaly加載故事板成的UITabBarController,但由於某種原因UITabBarItem不加載圖像或selectedImage所有除了第一個標籤不加載編程方式創建的UITabBarController子視圖不加載UITabBarItem圖像

Appearance when app first loads

但是,如果你點擊其他選項卡圖標負荷

After tabing through all tabs

故事板加載實例

import UIKit 

class ProfileViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let defaultImag = UIImage(named: "Profile")?.withRenderingMode(.alwaysOriginal) 
     let selectedImag = UIImage(named: "Profile_selected")?.withRenderingMode(.alwaysOriginal) 

     self.tabBarItem = UITabBarItem(title: nil, image: defaultImag, selectedImage: selectedImag) 
     self.tabBarItem.imageInsets = UIEdgeInsets(top: 10, left: 0, bottom: -10, right: 0); 
    } 

} 

的UITabBarController

import UIKit 

class TabBarViewController: UITabBarController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 


     let discoverStoryboard = UIStoryboard(name: "Discover", bundle: nil) 
     let discoverViewController = discoverStoryboard.instantiateInitialViewController()! as UIViewController 

     let favouritesStoryboard = UIStoryboard(name: "Favourites", bundle: nil) 
     let favouritesViewController = favouritesStoryboard.instantiateInitialViewController()! as! FavouritesViewController 

     let profileStoryboard = UIStoryboard(name: "Profile", bundle: nil) 
     let profileViewController = profileStoryboard.instantiateInitialViewController()! as UIViewController 

     self.viewControllers = [discoverViewController, favouritesViewController, profileViewController] 

    } 
} 
+0

圖像不加載? –

+0

我添加了屏幕截圖 –

回答

0
private func createTabForStoryBoard(storyBoardName: String, title: String, imageName :String, tag: Int) -> (UINavigationController) { 

     let storyboard = UIStoryboard(name: storyBoardName as String, bundle: nil) 
     let viewController = storyboard.instantiateInitialViewController() as! UIViewController 

     let image = UIImage(named: imageName) 
     let tabBarItem:UITabBarItem = UITabBarItem.init(title: NSLocalizedString(title, comment: "Tab Bar Names") , image: image, tag: 0) 

     viewController.tabBarItem = tabBarItem 
     return viewController 

    } 


    // MARK: Custom Methods 

    func setTabBarViewControllers() { 
     let discoverViewController = self.createTabForStoryBoard("Discover", title: "Discover", imageName: "<image-name>", tag: 1) 

     let favouritesViewController = self.createTabForStoryBoard("Favourites", title: "Favourites", imageName: "<image-name>", tag: 2) 

     let profileViewController = self.createTabForStoryBoard("Profile", title: "Profile", imageName: "<image-name>", tag: 3) 

     let tabBarViewControllers = [discoverViewController, favouritesViewController, profileViewController] 
     self.viewControllers = tabBarViewControllers 
    } 
+0

將UITabBarItem的Init移動到自定義的UITabBarController工作,但爲什麼它不工作,如果UIViewController已經用UITabBarItem初始化,它應該在加載Tab時讀取其值? –

相關問題