let count = CGFloat(tabBar.items!.count)
let itemSize = CGSize(width: tabBar.frame.size.width/count, height: tabBar.frame.height)
for (index, _) in tabBar.items!.enumerate() {
if index == 2 {
let xPosition = itemSize.width * CGFloat(index)
let backgroundColor = UIView.init(frame: CGRect.init(x: xPosition, y: 0, width: itemSize.width, height: itemSize.height))
backgroundColor.backgroundColor = UIColor.redColor()
tabBar.insertSubview(backgroundColor, atIndex: 1)
}
}
나는 얼마 전에 비슷한 효과를 구현하고, 위의 코드는 중요한 부분입니다.
갱신 1 : 당신은 또한 특정 tabBarItem의 선택 배경 색상을 변경하려면
코드는 아래의 일을 할 것입니다. UITabBarController를 하위 클래스로 만들고 tabBar:didSelectItem
메서드를 재정의해야합니다.
override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
let index: Int = tabBar.items!.indexOf(item)!
if index == 2 {
tabBar.selectionIndicatorImage = UIImage.fromColor(UIColor.greenColor(), size: CGSize.init(width: UIScreen.mainScreen().bounds.size.width/5, height: 49))
} else {
tabBar.selectionIndicatorImage = UIImage.fromColor(UIColor.snpPaleblueColor(), size: CGSize.init(width: UIScreen.mainScreen().bounds.size.width/5, height: 49))
}
tabBar.setNeedsDisplay()
}
static func fromColor(color: UIColor, size: CGSize) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let img = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return img
}
갱신 2 :
당신이하는 tabBarItem의 이미지와 selectedImage 색상을 변경 imageWithRenderingMode:
예는 다음과 같습니다 UIImage
의 방법을 사용하려면.
item.image = UIImage.init(named: "tabBarIcon-white").imageWithRenderingMode(.AlwaysOriginal)
item.selectedImage = UIImage.init(named: "tabBarIcon-blue").imageWithRenderingMode(.AlwaysOriginal)
당신은'self.tabBar.tintColor = UIColor (빨간색 다르게 그때 또한 가운데 버튼의 색조를 변경할 수 선택한 이미지 – Janmenjaya