简体   繁体   English

使用Tabbar iOS Swift

[英]Working with Tabbar ios Swift

I am working on UITabBar using Swift 4 where I want different icon for selected and un-selected tab. 我正在使用Swift 4在UITabBar工作,我想为所选和未选中的选项卡使用不同的图标。

But UITabBar is changing tintColor only and I am not able to set different image for selected and un-selected tab. 但是UITabBar仅更改了tintColor而我无法为选中和未选中的标签设置不同的图像。

So if it is possible to set different icon for selected and un-selected tab please let me know. 因此,如果可以为选中和未选中的标签设置不同的图标,请告诉我。

This is what I have tried : 这是我尝试过的:

let item1 = self.storyboard?.instantiateViewController(withIdentifier: "DashboardViewController")
let icon1 = UITabBarItem(title: "", image: UIImage(named: "first_unselected"), selectedImage: UIImage(named: "first_selected"))
item1?.tabBarItem = icon1

The problem is not in the way how you create an UITabBarItem - I've tested that and it works. 问题不在于您如何创建UITabBarItem -我已经对其进行了测试,并且可以正常工作。 So I guess the problem is where you set it up: 所以我想问题出在哪里进行设置:

let item1 = self.storyboard?.instantiateViewController(withIdentifier: "DashboardViewController")

You have to realize that this line of code will create a NEW instance of the DashboardViewController , which is NOT the one presented on the screen. 您必须意识到,这行代码将创建DashboardViewControllerNEW实例,而不是屏幕上显示的实例。 So unless somewhere later you present item1 , then of course those line of code will not have any impact on the screen. 因此,除非稍后您显示item1 ,否则这些代码行当然不会对屏幕产生任何影响。

What you want to do is to configure the instance that is presented on the screen (the one loaded automatically by the storyboards). 您要做的是配置屏幕上显示的实例(由情节提要自动加载的实例)。 I think the best and the easiest way is to add the configuration code into initializers of the DashboardViewController - this way ANY instance of the DashboardViewController will behave properly - so the one presented on the screen too. 我认为最好的和最简单的方法是将配置代码添加到的初始化DashboardViewController -这种方式的任何实例DashboardViewController会循规蹈矩-所以在屏幕上呈现的太之一。

Take the following code as an example: 以以下代码为例:

import UIKit

class DashboardViewController: UIViewController {

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

        initialize()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        initialize()
    }

    func initialize() {
        let icon1 = UITabBarItem(title: "", image: #imageLiteral(resourceName: "first_unselected"), selectedImage: #imageLiteral(resourceName: "first_selected"))
        self.tabBarItem = icon1
    }
}

According to the documentation you should use selectedImage property in UITabBarItem. 根据文档,您应该在UITabBarItem中使用selectedImage属性。

  let tabBarButton = UITabBarItem(title: "", image: 
  UIImage(named:"image"))
  tabBarButton.selectedImage: UIImage(named: "selected_image")

By default, the actual unselected and selected images are automatically >created from the alpha values in the source images. 默认情况下,将根据源图像中的Alpha值自动创建实际未选择和选择的图像。 To prevent system >coloring, provide images with alwaysOriginal. 为防止系统变色,请为图像提供alwaysOriginal。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM