Change image of UITabBarItem - ios

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)

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
}

How to add a UIView into navigationItem.backBarButtonItem

I am trying to achieve this
this is my code
let contactName=UILabel()
contactName.text=chat?.Name
contactName.font=UIFont(name: "System", size: 17)
contactName.sizeToFit()
let contactImg=UIImageView()
contactImg.image=UIImage(named: (chat?.Image)!)
contactImg.frame.size.width=20
contactImg.frame.size.height=20
contactImg.layer.cornerRadius=contactImg.frame.height/2
let backButtonView=UIView()
backButtonView.addSubview(contactImg)
backButtonView.addSubview(contactName)
backButtonView.frame.size.width=20+contactName.frame.width
backButtonView.frame.size.height=max(contactImg.frame.height,contactName.frame.height)
navigationItem.backBarButtonItem=UIBarButtonItem(customView: backButtonView)
but when I run app it shows the default backbarbutton
what am I doing wrong here please guide.
let profileImage = UIImage(named: "profile_image")
self.navigationController?.navigationBar.backIndicatorImage = profileImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = profileImage
self.navigationController?.navigationBar.backItem?.title = "Jhon"
Aditya is right, but here's a little more explanation. From the Apple docs:
When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.
https://developer.apple.com/documentation/uikit/uinavigationitem/1624958-backbarbuttonitem
Also, the UIBarButtonItem is "...specialized for placement on a toolbar or tab bar. You typically use Interface Builder to create and configure bar button items."
https://developer.apple.com/documentation/uikit/uibarbuttonitem

Swift how to change tintColor of backIndicatorImage

I have got this code in AppDelegate
U
INavigationBar.appearance().backIndicatorImage = #imageLiteral(resourceName: "backarrow")
And even though the image's color is white it shows it as blue.How can i change the tintcolor of this image?
Go to Assets.xcassets
Select your image backarrow
Show the Attributes Inspector
Change Render As value form Default to Original Image
Swift 5.x & Xcode 12 Solution:
let image = UIImage(named: "abc")!.withRenderingMode(.alwaysOriginal)
navigationBar.backIndicatorImage = image
navigationBar.backIndicatorTransitionMaskImage = image

IOS - tab bar item image modifications

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

Resources