How to select tab bar item without selecting it - ios

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

Related

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 set bar button item background image globally in iOS 13

iOS 13 has a whole new set of classes for configuring navigation bars and bar button items:
UIBarAppearance
UINavigationBarAppearance
UIBarButtonItemAppearance
UIBarButtonItemStateAppearance
How do I use these to give bar button items a global appearance? Let's say I want all bar button items to have a common background image. I used to say:
UIBarButtonItem.appearance().setBackgroundImage(im, for:.normal, barMetrics:.default)
This seems to be just what is superseded by these new classes. So what's the right way now?
It's verbose, but in some ways it's clearer. You can configure a navigation bar appearance and assign it to the navigation bar's standardAppearance through the proxy:
let app = UIBarButtonItemAppearance()
app.normal.backgroundImage = im
let navbarapp = UINavigationBarAppearance()
navbarapp.configureWithOpaqueBackground()
navbarapp.buttonAppearance = app
UINavigationBar.appearance().standardAppearance = navbarapp
The only problem with that is that it assigns the same background image to back button items. So if that's not desired, you have to code defensively, assigning back button items an empty (not nil) image:
let app = UIBarButtonItemAppearance()
app.normal.backgroundImage = im
let navbarapp = UINavigationBarAppearance()
navbarapp.configureWithOpaqueBackground()
navbarapp.buttonAppearance = app
let back = UIBarButtonItemAppearance()
back.normal.backgroundImage = UIImage() // prevent back button item
navbarapp.backButtonAppearance = back
UINavigationBar.appearance().standardAppearance = navbarapp

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)

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

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