How to programmatically change Tab Bar Item image and font when selected? - swift

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")

Related

How to make tab bar controller update?

I'm rather new to swift and I am currently building an application that shows movies and TV shows that are in theatres / just airing.
Initially I had it so that there are only 2 tabs in the tab bar controller and it shows newest movies and recently aired TV shows. In order to enhance user experience I created a menu through which the use could pick up to 4 different categories (e.g. Top rated movies, newly released movies, popular movies and so on).
I have created an array so that the tab bar controller knows where to get the data from to see the movies and the TV shows. When the user selects the new categories, the array is updated, but the tab bar controller does not update its tabs. When the app is restarted though, the tabs reflect the newly selected categories.
I know if this were a tableview I could call tableview.reload() but I'm not sure how to do this for a tab bar controller. Please help me out and thanks in advance!
Here is the link to my project
Kevin you can make one function in appDelegate and make the tabbar a rootview controller
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let nowPlayingNavigationController = storyboard.instantiateViewControllerWithIdentifier("FlicksNavigationController") as! UINavigationController
let nowPlayingViewController = nowPlayingNavigationController.topViewController as! MoviesViewController
nowPlayingViewController.endpoint = "now_playing"
nowPlayingNavigationController.tabBarItem.title = "YOUR SELECTED TITLE"
nowPlayingNavigationController.tabBarItem.image = UIImage(named: "popular")
nowPlayingNavigationController.navigationBar.barStyle = .BlackTranslucent
let topRatedNavigationController = storyboard.instantiateViewControllerWithIdentifier("FlicksNavigationController") as! UINavigationController
let topRatedViewController = topRatedNavigationController.topViewController as! MoviesViewController
topRatedViewController.endpoint = "top_rated"
topRatedNavigationController.tabBarItem.title = "YOUR SELECTED TITLE"
topRatedNavigationController.tabBarItem.image = UIImage(named: "topRated")
topRatedNavigationController.navigationBar.barStyle = .BlackTranslucent
window?.rootViewController = tabBarController
You can write this code in that function and make it root viewcontroller and make the title change,
Or
in your Tabbarviewcontroller.swift you can use postnotification and update it accordingly.

Changing background colour of each tab in custom tab bar

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.

Swift - bar button item image / set size and width

Feel like I've been wasting a lot of time on this one and would love some help. I'm trying to use a custom menu icon (20px x 20px) in my barButtonItem. The problem it that is scales to fill the bar button item and is distorted.
Normally this shouldn't be too complicated. But I'm using tabbed view controllers and not a navigation controller. The navigation bar I have dropped into the view controller is just an outlet.
Normally I could do something like this:
let image = UIImage(named: "menu_white")
let frame = CGRectMake(0, 0, image!.size.width, image!.size.height)
let button = UIButton()
button.frame = frame
button.setImage(image, forState: .Normal)
let rightMenuButton = UIBarButtonItem(customView: button)
self.navigationItem.setRightBarButtonItem(rightMenuButton, animated: true)
But this doesn't work because self.navigationItem doesn't actually refer to anything in the view controller.
I need to somehow get the image into the outlet programmatically but setRightBarButtonItem is not a method of UINavigationBar.
#IBOutlet weak var navBar: UINavigationBar!
Would really appreciate some help if anyone's got some ideas.
The way a "loose" navigation bar works is that you configure a UINavigationItem and set an array containing that to be the navigation bar's items, as in this example code (from my book):
let ni = UINavigationItem(title: "Tinker")
let b = UIBarButtonItem(title: "Evers", style: .Plain, target: self, action: "pushNext:")
ni.rightBarButtonItem = b
self.navbar.items = [ni]
So, you could presumably do something like that.
The self.navigationItem thing is merely a way of causing that to be done automatically in the special situation where you're using a UINavigationController. But you say that you're not doing that. (Of course, you could do that; there's no law that says you have to use a navigation controller to do any actual navigation. I often use a navigation controller just to get the navigation bar, because it's a good place to put things like little menu buttons.)

UITabBarController customisation page top bar colour swift

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

Tab Bar - custom images without titles

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.