如何更改TabBarItem背景颜色

时间:2016-02-18 17:13:51

标签: ios swift tabbar uitabbaritem

我试图更改 TabBarItem 的背景,或制作占据所有空间的整个图像。

如下例所示:

Tabbar

你有一个想法如何在swift中做到这一点

2 个答案:

答案 0 :(得分:0)

夫特:

//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")

答案 1 :(得分:0)

swift 4.2中与iOS 12.1

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)
}