IOS - tab bar item image modifications - ios

I want to change the default color of the tab bar item image to be the original color of the image (black) as opposed to a gray when it's not selected.
I also want to change the tab bar item image to a filled version once it's selected.
Last thing is the position.. It seems to expect text under it so it's not centered, how do I center it vertically and possibly make it smaller?
I'm currently setting it this way:
let profileNavController = UINavigationController(rootViewController: profileController)
profileNavController.tabBarItem.image = UIImage(named: "icon_tab_user")
This is how it looks selected and unselected:

From apple we know,
By default, the actual unselected and selected images are automatically created from the alpha values in the source images. To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal.
Take a look here.
Changing tab bar item image and text color iOS

I was able to change the highlighted and unhighlighted images as well as the position with this:
let profileNavController = UINavigationController(rootViewController: profileController)
let profileTabBarItem = UITabBarItem(title: nil, image: UIImage(named: "icon_tab_user")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), selectedImage: UIImage(named: "icon_tab_user_highlighted"))
profileTabBarItem.imageInsets = UIEdgeInsetsMake(5.5, 0, -5.5, 0)
profileNavController.tabBarItem = profileTabBarItem

Related

How to select tab bar item without selecting it

I am using swift and I have a problem with the tab bar. I want to select a tab bar item, but without open it, what I mean: I want to change its color like it is selected but without the selection is it possible to be done. Here is a image: https://imgur.com/a/IdPituS , I want the top left item (burger menu item) color to be like the first one and the first one color to be light (like not selected).
Try to edit the image of the item you want to highlight (selecting without selecting).
You can try something like this:
let image = shouldHighlight ? UIImage(named: "highlightItem") : UIImage(named: "normalStyle")
myViewController.tabBarItem = UITabBarItem(title: "text", image: image, selectedImage: UIImage(named: "selectedImage"))
Then you update the viewControllers property in you tab bar viewController
myTabViewController.viewControllers
You can pre-select the tabBar item
like this:
tabBarController?.selectedIndex = 0 //select whatever You want
and
colorTint the unselected index
tabBar = UITabBarController()
tabBar?.tabBar.barTintColor = UIColor.white //bar Color
tabBar?.tabBar.tintColor = .red //Icons color
tabBar?.tabBar.unselectedItemTintColor = .white //Unselected item Colors

Set TabBar Item Disabled Color

My Current tabBar :
Expected tabbar with disabled tabs(ignore the language its for sample purpose):
I am able to disable items from my first tab bar image by using this :
self.isEnabled = false
For Not loggedin users i disabled these items but i want to change their color to something dull. How to do that.
Note : I am not asking for changing color in unselected state. I am asking only for the disabled state.
Add your following tab bar images as white.
Like
And in you MainStoryboard select your Main TabBarController, then change the image tint to Red (whatever color you need to show the image color)
Then you can see that the unselected TabBar Items color looks disabled.
To make look like disabled tabbar item.
if let arrayOfTabBarItems = tabBarViewController.tabBar.items as! AnyObject as? NSArray,tabBarItem = arrayOfTabBarItems[2] as? UITabBarItem {
tabBarItem.enabled = false
}

Display only text without image for UITabBarItem?

I want to display only title for tabbar item without image. Is it possible? When I tried setting
let tabItem = UITabBarItem()
tabItem.title = "Tab1"
tabItem.selectedImage = nil
tabItem.image = nil
tabViewController?.tabBarItem = tabItem
I'm just getting a blank space which is tapable, but no title or image.
simplest way is remove the images from the tab bar and set the custom offset vertical.as shown in the image. or else follow the link that TJ3n priveded remove the images in the UITabBarItem and aligned vertically the title.
In case you want it to be done programmatically:
tabItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -16)

Set multicolor tab bar icon instead of monochrome icon

I need a way to have multicolor tab bar icons, but for some reason, iOS keeps drawing the icon in a monochrome fashion.
I tried various methods such as including this line of code: tabBarItem.image = #imageLiteral(resourceName:"routineIcon").withRenderingMode(.alwaysOriginal) in the viewDidLoad of my first view controller but I had no luck.
Is there any way I can retain color information in a tab bar icon without making it monochrome? It looks a thousand times better when it's multicolored.
Thanks,
Harish
You need to create a new UITabBarItem and assign it to the tabBarItem property of your view controller.
// In viewDidLoad()
self.tabBarItem = UITabBarItem(title: "Hello world!", image: #imageLiteral(resourceName: "routineIcon").withRenderingMode(.alwaysOriginal), tag: 0)
Also, if you're using an assets catalog (which you should), notice that in your asset settings you have a Render As setting which you can set to Always Original.
You can then ommit the .withRenderingMode(.alwaysOriginal) when using your image.
// In viewDidLoad()
self.tabBarItem = UITabBarItem(title: "Hello world!", image: #imageLiteral(resourceName: "routineIcon"), tag: 0)
There is also an initializer that takes a selected image if you have one
// In viewDidLoad()
self.tabBarItem = UITabBarItem(title: "Hello world!", image: #imageLiteral(resourceName: "routineIcon"), selectedImage: #imageLiteral(resourceName: "routineIconSelected"))
Note : As a general rule, modifying system UIBarItem objects (UITabBarItem for tab bars and UIBarButtonItem for navigation bars or tool bars) often doesn't work and you need to create a new one.
System bar items are the one created with init(tabBarSystemItem:tag:) (for UITabBarItem) or init(barButtonSystemItem:target:action:) (for UIBarButtonItem).
When using a storyboard, you can decide to use either a system bar item or a custom one.

Change image of UITabBarItem

I need to set a different image for the more button in the UITabBar in Swift 2.
I tried this but it doesn't work.
let more = tabBarController.moreNavigationController.tabBarItem
more.image = UIImage(named: "squadra")?.imageWithRenderingMode(.AlwaysOriginal)
Really thanks.
You can do it from interface builder as follows:
Open document outline and select tab bar item as follows:
Then change image property of bar item from attribute inspector to appropriate xcasset as follows:
Finally your image of tab bar item will be changed
tabBarController.tabBar.items?.first?.image = UIImage(named: "squadra")?.imageWithRenderingMode(.AlwaysOriginal)

Resources