Hey i would like to change the background Color of my center tab bar item like instagram in swift, anyone who knows how it works. I googled but can't find any helpful answer.
Asked
Active
Viewed 1,610 times
0
-
1Set a background image of Tabbar control 'setBackgroundImage' with center color changed, make sure about sizes for color ur center/perticuler tab background. – Viral Savaj Sep 02 '15 at 10:59
2 Answers
1
You can use the following code :
// Add background color to middle tabBarItem
let itemIndex = 2
let bgColor = UIColor(red: 0.08, green: 0.726, blue: 0.702, alpha: 1.0)
let itemWidth = tabBar.frame.width / CGFloat(tabBar.items!.count)
let bgView = UIView(frame: CGRectMake(itemWidth * itemIndex, 0, itemWidth, tabBar.frame.height))
bgView.backgroundColor = bgColor
tabBar.insertSubview(bgView, atIndex: 0)
For reference you can look into these Stackoverflow posts :
-
If I use this code I get the error "Cannot invoke initializer for type 'UIView' with an argument list of type '(frame: CGRect)'" – telip007 Sep 02 '15 at 11:54
-
-
or make following change : UIView(frame: CGRectMake(CGFloat (itemWidth * itemIndex), 0, itemWidth, tabBar.frame.height)) – Munahil Sep 02 '15 at 12:03
-
Try this : UIView(frame: CGRectMake(CGFloat (CGFloat(itemWidth) * itemIndex), 0, itemWidth, tabBar.frame.height)) – Munahil Sep 02 '15 at 12:10
-
Solved it! Solution : let bgView = UIView(frame: CGRect(x: CGFloat(itemWidth * CGFloat(itemIndex)),y: 0, width: CGFloat(itemWidth),height:(tabBarController?.tabBar.frame.height)!)) – telip007 Sep 02 '15 at 12:16
-
-
-
-
-2
Change Background color of tabBarItem or use Background Image and change contest according to requirement

baydi
- 1,003
- 6
- 11
-
How does this even attempt to answer the question. This is clearly a comment and not an answer. – Popeye Sep 02 '15 at 11:14
-
The Problem is I don't know how to change the background color, there is no type background color for tab bar item – telip007 Sep 02 '15 at 11:26
-
Use this link to perform your process in swift http://stackoverflow.com/questions/30041127/ios-8-tab-bar-item-background-colour – baydi Sep 02 '15 at 11:37