How can I change the background colour for each tab in an iOS application in Swift, as shown in the image?
This can be implemented with the help of setting images on each tab:
let aryTabImages = ["home-icon.png","help-icon.png","photos-icon.png","cart-icon.png","search-icon.png"]
Assign to each tabbar item:
for (index, item) in self.tabBarController!.tabBar.items!.enumerate() {
item.image = UIImage(named: aryTabImages.objectAtIndex(index) as! String)!.imageWithRenderingMode(.AlwaysOriginal)
item.selectedImage = UIImage(named: aryTabImages.objectAtIndex(index) as! String)!.imageWithRenderingMode(.AlwaysOriginal)
}
Also Make sure about:
Tabbar item height for each IOS device
Tint color correctly selected of UITabbarController's tabbar.
Related
I would like to change the weight of my Tab Bar Item and font when it is selected. I assume this can be done by changing the Tab Bar Item Image and text when the associated tab is being selected. Apart from adjusting the tintcolor of highlighted tab bar items, I can't seem to find any information customising selected Tab Bar Items online even though most apps (including Instagram) do it. How is this done?
My current 5 tab bars are created in a UITabBarController Class and follow an almost identical formula like this:
let homeController = HomeViewController()
homeController.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named: "home"), tag: 1)
let nav1 = UINavigationController(rootViewController: homeController)
viewControllers = [nav1, nav2, nav3, nav4, nav5]
Programmatically set selected UITabbarItem's image:
let tabBarItem = UITabBarItem(title: "title", image: UIImage(named: "defaultImage"), selectedImage: UIImage(named: "selectedImage"))
You can't as easily set selected UITabbarItem's font though. You'd need to create your own UITabBarController as shown in this SO thread.
In order to change the image I simply enter this code in the associated view controller class (in my case they are individuals swift files)
so in viewDidLoad of class HomeViewController: UIViewController {
self.tabBarItem.selectedImage = UIImage(named: "yourImageName")
I have a UITabBar and I want to make it blurred. I wrote the following code:
import UIKit
class TabBarController:UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let blur = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurView = UIVisualEffectView(effect: blur)
blurView.frame = self.view.bounds
blurView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.view.layer.insertSublayer(blurView, atIndex: 0)
}
}
but somehow the last line throws error:
Cannot convert value of type 'UIVisualEffectView' to expected argument
type 'CALayer'
how can I fix that?
I changed the last line to:
self.tabBar.addSubview(blurView)
but now the whole tabbar is blurred (even with icons and they are not visible). When I changed this line to:
self.tabBar.sendSubviewToBack(blurView)
then the tabbar is visible, but not blurred. I want to achieve effect from accepted answer from here Black background on transparent UITabBar but here it is uitabbar and I'm using uitabbarcontroller... Can you help me with applying blur in my case?
You just add the blur view as a subview:
self.view.addSubview(blurView)
Since you just want to blue the tab bar and this class is a tab bar controller, you can do:
self.tabBar.addSubview(blueView)
You also need to change the frame:
blurView.frame = self.tabBar.bounds
why don't you just use the barTintColor property on your TabBarController?
self.tabBar.translucent = true
self.tabBar.barTintColor = UIColor.blackColor()
You don't even need to subclass UITabBarController. You can call this on any UIViewController.
self.tabBarController?.tabBar.translucent = true
self.tabBarController?.tabBar.barTintColor = UIColor.blackColor()
If I understood correctly from the following comment that you posted, you want to change the UITabBar to be black in colour but still blurred.
And yes, I noticed that the UITabBarController is blurred by default, but I would like to make it blurred with specific style (.Dark).
Doing this since iOS 7 has actually become quite easy. Simply change the barStyle of your UITabBar to .black. Put the following code in your UIViewController's viewDidLoad method (note that UITabBar is translucent by default, so you don't need to specify that again).
tabBarController?.tabBar.barStyle = .black
If you want to set it back to the regular, white barStyle, change it back to .default.
tabBarController?.tabBar.barStyle = .default
You may even do this from within Interface Builder by selecting the Tab Bar in your UITabBarController's hierarchy and changing its Style to Black.
I have a solution, all you need is configure your UITabBar as following:
// next code will make tabBar fully transparent
tabBar.isTranslucent = true
tabBar.backgroundImage = UIImage()
tabBar.shadowImage = UIImage() // add this if you want remove tabBar separator
tabBar.barTintColor = .clear
tabBar.backgroundColor = .black // here is your tabBar color
tabBar.layer.backgroundColor = UIColor.clear.cgColor
If you want to add blur, do this:
let blurEffect = UIBlurEffect(style: .dark) // here you can change blur style
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = tabBar.bounds
blurView.autoresizingMask = .flexibleWidth
tabBar.insertSubview(blurView, at: 0)
As a result:
Attach bottom constraint to the bottom of the view instead of Safe Area
It just might not be a problem with your TabBar but with tableView constraints.
Tab bar is blurred by default.
I have icons in my assets that I want to use for my tab bar, 2 for each tab (one representing not highlighted i.e just an outline, and one representing highlighted i.e all filled in). The idea is to use the outlined icons for the tabs that aren't currently selected, and the filled in one for the tab that is currently selected. How can I go about doing this?
Just make an Class for your Tabbar, for example:
class MainTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
for item in self.tabBar.items! as [UITabBarItem] {
// loop through all of your elements in TabBar
if let image = item.image {
item.selectedImage = your Selected Image
item.image = your base Image
}
}
}
You could store all your images in an Array containing UIImage, like:
let TabImages = [UIImage]()
let HighlightedImages = [UIImage]()
And then set them in your for each loop.
var tabBarController = self.window!.rootViewController as UITabBarController
let tabItems = tabBarController.tabBar.items as [UITabBarItem]
tabItems[2].selectedImage = UIImage(named: "tabImage1_Selected")
How do I change the colour of the top bar of the customisation page of the more view controller. Please see the linked image. Sorry I can't post image here because of my low reputation.
Screenshot Image
Edited with more info:
I have managed to change the background color using the following code. But cant change the color of the top bar.
func tabBarController(tabBarController: UITabBarController, willBeginCustomizingViewControllers viewControllers: [AnyObject]) {
var editView : UIView = tabBarController.view.subviews[1] as! UIView
editView.backgroundColor = UIColor.blackColor()
}
Basically 2 Ways you can achieve this if you are using Global Unique colour in the app for All navigation bar
Use this Solution on App Launch:
UINavigationBar.appearance().barTintColor = UIColor.redColor()
Or if you want to change the Color of More navigationcontroller only then
Use this Solution by getting the Tabbar Reference :
self.tabBarController?.moreNavigationController.navigationBar.barTintColor = UIColor.greenColor()
You can use second solution in the First viewcontroller of the Tab, because that contains your tabbar reference
I want to set my tab bar to have custom images but XCode insists in leaving the space for the tab bar item title text leaving my images positioned too high in the tab bar. I've tried the following code in my TabBarController.swift viewDidLoad function:
let tabItems = tabBar.items as [UITabBarItem]
tabItems[0].title = nil
tabItems[0].selectedImage = UIImage(named: "HomeWhiteIcon")
tabItems[1].title = "Database"
tabItems[1].selectedImage = UIImage(named: "Second")
tabItems[2].title = nil
tabItems[2].selectedImage = UIImage(named: "SettingsWhiteIcon")
tabItems[3].title = nil
tabItems[3].selectedImage = UIImage(named: "ReportsWhiteIcon")
However, although no title is displayed the images are positioned too high as pic below (please ignore database icon - I have not set this icon yet.
Here's a random stab as swift is not my strong suit, but perhaps the database icon having a title is causing the entire title row to not collapse. Try removing the title from the database icon.
Otherwise have you seen the setTitlePositionAdjustment method? Perhaps adjust the title's position up into the icon and the space below will go away.