Ios 如何更改TabBarItem背景色

Ios 如何更改TabBarItem背景色,ios,swift,tabbar,uitabbaritem,Ios,Swift,Tabbar,Uitabbaritem,我试图改变一个TabBarItem的背景,或者制作一个占据所有空间的整体图像 如以下示例所示: 您知道如何在swift中实现这一点吗 //change icon and title color UITabBar.appearance().tintColor = UIColor.redColor() //change background default color UITabBar.appearance().barTintColor = UIColor.blackColor() //cha

我试图改变一个TabBarItem的背景,或者制作一个占据所有空间的整体图像

如以下示例所示:

您知道如何在swift中实现这一点吗

//change icon and title color
UITabBar.appearance().tintColor = UIColor.redColor()

//change background default color
UITabBar.appearance().barTintColor = UIColor.blackColor()

//change selected background image
UITabBar.appearance().selectionIndicatorImage = UIImage(named: "tabSelected")

在带有iOS 12.1的swift 4.2中

func setUpSelectionIndicatorImage(withColors colors: [UIColor]) {
    //Make selection indicator image from color and set it to tabbar
    let singleTabWidth: CGFloat = self.tabBar.frame.size.width / CGFloat(self.tabBar.items?.count ?? 1)
    let height = DeviceType.IS_IPHONE_X ? 55 : self.tabBar.frame.size.height
    let singleTabSize = CGSize(width:singleTabWidth , height: height)
    let edgeInsets = DeviceType.IS_IPHONE_X ? UIEdgeInsets(top: 1, left: 0, bottom: 0, right: 0) : .zero
    self.tabBar.selectionIndicatorImage = UIImage.gradient(size: singleTabSize, colors: colors)?.resizableImage(withCapInsets: edgeInsets)
}

 override func viewDidLayoutSubviews() {
    let colors = [UIColor.white, UIColor.green]
    setUpSelectionIndicatorImage(withColors: colors)
}
你可以看看这个和那个,看看这个: