2016-09-27 161 views
0

我已將一個視圖控制器&嵌入到導航控制器中,並再次嵌入到標籤欄控制器中。當我試圖通過設置故事板圖像,圖像不會出現在標籤欄icon.here映像名稱是25如何在swift中的標籤欄項目中設置圖像?

enter image description here

enter image description here

請建議我,我該怎麼辦?我怎樣才能以編程方式執行它?我應該爲此採取適當的圖像大小?提前致謝。

+0

不,不需要編程..也可以通過上述方法完成。 –

+0

但爲什麼這不適用於我的項目? – KhanShaheb

+0

你能顯示輸出截圖嗎? –

回答

1

設置兩個圖像 - 用於選擇/選中狀態

See This

+0

使用透明圖像我不喜歡你,但同樣的問題。這是我的電腦的錯誤或問題,我無法理解。他們的圖像,圖像大小的任何命名約定? – KhanShaheb

+0

當我使用iconbeast或icons8中的圖標時,它們會很好地顯示,但是當我使用我的圖像時,它們不受支持。任何想法? – KhanShaheb

+0

[見這(https://github.com/mho000/MHImageTabBar),並檢查什麼是你的代碼 – iDeveloper

5

添加AppDelegate類:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
{ 
    window=UIWindow(frame: UIScreen.main.bounds) 
    self.window?.rootViewController = setTabbar() 
    self.window?.makeKeyAndVisible() 
    window?.backgroundColor=UIColor.white 
    return true 
} 

func setTabbar() -> UITabBarController 
{ 
    let storyboard = UIStoryboard(name: "Main", bundle: nil) 
    let tabbarcntrl = UITabBarController() 

    let Home = storyboard.instantiateViewController(withIdentifier: "HomeView") // 1st tab bar viewcontroller 
    let Followed = storyboard.instantiateViewController(withIdentifier: "FollowedView") // 2nd tab bar viewcontroller 
    let Message = storyboard.instantiateViewController(withIdentifier: "MessageView") // 3rd tab bar viewcontroller 

    // all viewcontroller embedded navigationbar 
    let nvHome = UINavigationController(rootViewController: Home) 
    let nvFollowed = UINavigationController(rootViewController: Followed) 
    let nvMessage = UINavigationController(rootViewController: Message) 

    // all viewcontroller navigationbar hidden 
    nvHome.setNavigationBarHidden(true, animated: false) 
    nvFollowed.setNavigationBarHidden(true, animated: false) 
    nvMessage.setNavigationBarHidden(true, animated: false) 

    tabbarcntrl.viewControllers = [nvHome,nvFollowed,nvMessage] 

    let tabbar = tabbarcntrl.tabBar 
    tabbar.barTintColor = UIColor.black 
    tabbar.backgroundColor = UIColor.black 
    tabbar.tintColor = UIColor(red: 43/255, green: 180/255, blue: 0/255, alpha: 1) 

    //UITabBar.appearance().tintColor = UIColor.white 
    let attributes = [NSFontAttributeName:UIFont(name: "Montserrat-Light", size: 10)!,NSForegroundColorAttributeName:UIColor.white] 
    let attributes1 = [NSFontAttributeName:UIFont(name: "Montserrat-Light", size: 10)!,NSForegroundColorAttributeName:UIColor(red: 43/255, green: 180/255, blue: 0/255, alpha: 1)] 

    UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .normal) 
    UITabBarItem.appearance().setTitleTextAttributes(attributes1, for: .selected) 


    let tabHome = tabbar.items![0] 
    tabHome.title = "Home" // tabbar titlee 
    tabHome.image=UIImage(named: "icon_home.png")?.withRenderingMode(.alwaysOriginal) // deselect image 
    tabHome.selectedImage = UIImage(named: "icon_home.png")?.withRenderingMode(.alwaysOriginal) // select image 
    tabHome.titlePositionAdjustment.vertical = tabHome.titlePositionAdjustment.vertical-4 // title position change 

    let tabFoll = tabbar.items![1] 
    tabFoll.title = "Followed" 
    tabFoll.image=UIImage(named: "icon_fold.png")?.withRenderingMode(.alwaysOriginal) 
    tabFoll.selectedImage=UIImage(named: "icon_fold.png")?.withRenderingMode(.alwaysOriginal) 
    tabFoll.titlePositionAdjustment.vertical = tabFoll.titlePositionAdjustment.vertical-4 

    let tabMsg = tabbar.items![3] 
    tabMsg.title = "Message" 
    tabMsg.image=UIImage(named: "icon_mail.png")?.withRenderingMode(.alwaysOriginal) 
    tabMsg.selectedImage=UIImage(named: "icon_mail.png")?.withRenderingMode(.alwaysOriginal) 
    tabMsg.titlePositionAdjustment.vertical = tabMsg.titlePositionAdjustment.vertical-4 

    return tabbarcntrl 
} 
+0

錯誤,我會把你的FUNC setTabbar() - >的UITabBarController {}功能??? – KhanShaheb

+0

在AppDelegate類 –

+0

感謝您的回答:) – KhanShaheb

2

你正在做的一切正道的東西,但唯一的問題是你的tabbaritem圖像的大小不正確。只需查看此表以查看tabbaritem圖像的實際大小。

enter image description here

+0

當我從圖像圖標下載圖像,其屬性顯示30x30 – KhanShaheb

+0

您需要創建1x,2x,3x Tabbar icon.So適當的資產爲您的圖像是:30x30 - 1x, 60x60 - 2x,90x90 - 3x –

+0

(約75 x 75)是什麼意思? – KhanShaheb

4

在你MainTabbarViewController

綁定您的TabBar的出口:

@IBOutlet weak var myTabBar: UITabBar? 

override func viewDidLoad() { 
     super.viewDidLoad() 

     myTabBar?.tintColor = UIColor.white 
     tabBarItem.title = "" 

     setTabBarItems() 

} 

設置的TabBar以下項目在這裏定義的方法:

func setTabBarItems(){ 

     let myTabBarItem1 = (self.tabBar.items?[0])! as UITabBarItem 
     myTabBarItem1.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
     myTabBarItem1.selectedImage = UIImage(named: "Selected ")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
     myTabBarItem1.title = "" 
     myTabBarItem1.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0) 

     let myTabBarItem2 = (self.tabBar.items?[1])! as UITabBarItem 
     myTabBarItem2.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
     myTabBarItem2.selectedImage = UIImage(named: "Selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
     myTabBarItem2.title = "" 
     myTabBarItem2.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0) 


     let myTabBarItem3 = (self.tabBar.items?[2])! as UITabBarItem 
     myTabBarItem3.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
     myTabBarItem3.selectedImage = UIImage(named: "Selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
     myTabBarItem3.title = "" 
     myTabBarItem3.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0) 

     let myTabBarItem4 = (self.tabBar.items?[3])! as UITabBarItem 
     myTabBarItem4.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
     myTabBarItem4.selectedImage = UIImage(named: "Selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) 
     myTabBarItem4.title = "" 
     myTabBarItem4.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0) 

} 

CHEERS!

相關問題