I am using UITabBarController with 5 ViewController each ViewController have own UINavigationController as below:
I have added tabar image like
As seen from above image when TabBarItem is selected just blue image i can see...i wanted to show original image for selected tabaritem.
I had seen the example of setting property UIImageRenderingModeAlwaysOriginal. but i can not see anything on story board.
Below is the way I push TabBarController:
HomeViewController *vcHome = (HomeViewController*)[[UIStoryboard storyboardWithName:#"NexTabBar" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"ID_HOME_VC"];
vcHome.delegate = self;
[self.navigationController pushViewController:vcHome animated:YES];
How can I use the property UIImageRenderingModeAlwaysOriginal here so that I can see original images when particular TabBarItem is selected?
or if any other way is there with ios10.
Here is how I create tabBarItem for myTabBar
let storyboard = UIStoryboard(name: storyboardIdentifier, bundle: nil)
let vc:UIViewController? = storyboard.instantiateInitialViewController();
let selectedImage = UIImage(named: imageName)
let notSelectedImage = UIImage(named:selectedImageName)?.withRenderingMode(.alwaysOriginal)
let item = UITabBarItem(title: title, image: notSelectedImage, selectedImage: selectedImage)
vc!.tabBarItem = item
Objective C version:
UIStoryboard *sb = [UIStoryboard storyboardWithName:storyboardIdentifier bundle:nil];
UIViewController* vc = sb.instantiateInitialViewController;
UIImage* selectedImage = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage* notSelectedimage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UITabBarItem* item = [[UITabBarItem alloc]initWithTitle:title image:notSelectedimage selectedImage:selectedImage];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
vc.tabBarItem = item;
return vc;
If this did not help try to change also tabBar tints colors
self.tabBar.barTintColor = .orange
// set color of selected icons and text to white
self.tabBar.tintColor = .white
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .selected)
// set color of unselected text to gray
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.gray], for: .normal)
Related
I have UITabBar controller
let tabBarController = UITabBarController()
tabBarController.viewControllers = [todayViewController, forecastViewController]
window.rootViewController = tabBarController
window.makeKeyAndVisible()
tabBarController.selectedIndex = 0
inside todayViewController:
tabBarItem = UITabBarItem(title: "Today", image: #imageLiteral(resourceName: "TodayTabBarItem"), selectedImage: #imageLiteral(resourceName: "SunTabBarItemSelected"))
inside forecastViewController:
tabBarItem = UITabBarItem(title: "Forecast", image: #imageLiteral(resourceName: "ForecastTabBarItem"), selectedImage: #imageLiteral(resourceName: "ForecastTabBarItemSelected"))
When I start the app UITabBarItem of the first view controller is inactive. When I tap on it or on another one it becomes active (highlighted text and right image)
What should I do to make UITabBarItem active on app start?
Try tabBarItem = UITabBarItem not during the call to viewDidLoad, but when creating ViewContrller - in init
I have added MMDrawerController to my project,before using MMDrawerController the navigation bar was appearing as desired in black color and the bar button item was also visible but when I added MMDrawerController to the project ,the navigation bar shows grey color rather than showing black and the bar button item also doesn't come .Kindly help me to get the navigation bar in black color and bar button item also visible.Thanks in advance!
//MARK: function for adding MMDrawer controller
func buildNavigationDrawerInterface () {
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let centerVC = mainStoryBoard.instantiateViewController(withIdentifier: "tabbar") as!UITabBarController
centerVC.tabBar.barTintColor = UIColor.black
let leftVCs = mainStoryBoard.instantiateViewController(withIdentifier: "LeftViewController") as! LeftViewController
let leftSideNav = UINavigationController(rootViewController: leftVCs)
let centerSideNav = UINavigationController(rootViewController: centerVC)
drawerContainer = MMDrawerController(center: centerSideNav, leftDrawerViewController: leftSideNav)
drawerContainer!.openDrawerGestureModeMask = MMOpenDrawerGestureMode.panningCenterView
drawerContainer!.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.panningCenterView
//centerContainer?.setDrawerVisualStateBlock(MMDrawerVisualState.swingingDoorVisualStateBlock())
window!.rootViewController = drawerContainer
window!.makeKeyAndVisible()
}
I need to show the selection image in tabbar item without the title.
Is there a possibility to hide the title at the time of selection?
here is my code
let icon1 = UITabBarItem(title: "Title1", image: UIImage(named: "homenormal")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "home")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal))
let icon2 = UITabBarItem(title: "Title2", image: UIImage(named: "usernormal")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "user")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal))
dashboard = DashboardViewController(nibName: CommonClass.DeviceType.IS_IPAD ? "DashboardViewControlleriPad" : "DashboardViewController", bundle: nil)
userMain = UserMainViewController (nibName : CommonClass.DeviceType.IS_IPAD ? "UserMainViewControlleriPad" : "UserMainViewController", bundle : nil )
tabbar = UITabBarController()
tabbar!.viewControllers = [dashboard!, userMain!]
dashboard!.tabBarItem = icon1
userMain!.tabBarItem = icon2
window!.rootViewController = tabbar
window?.makeKeyAndVisible()
The following code in my appDelegate works for Objective-C to show selected state of custom UITabBar Items. Despite my best efforts, I cannot figure out how to translate this code to Swift. Can someone point me in the right direction?
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]]; //make all text and icons in tab bar the system blue font
tabBarItem1.selectedImage = [UIImage imageNamed:#"815-car-selected#2x.png"];
tabBarItem2.selectedImage = [UIImage imageNamed:#"742-wrench-selected#2x.png"];
tabBarItem3.selectedImage = [UIImage imageNamed:#"710-folder-selected#2x.png"];
tabBarItem4.selectedImage = [UIImage imageNamed:#"724-info-selected#2x.png"];
Thank you.
OK, so first off, I assume you're setting your Image and Selected Image in your Story Board, and are running into the issue where the selected image doesn't show up (based on the code sample you provided). Here's what I have in Swift 1.2 (I assume this all works in earlier versions as well). Its based on the response from ad121, but with changes I needed to make it work correctly. And note, you'll want this in your AppDelegate in case you're unsure where this goes.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
// Type casting in swift is "as Type", you'll need to unwrap optionals however.
let tabBarController = self.window!.rootViewController as! UITabBarController
let tabBar = tabBarController.tabBar as UITabBar
// I prefer to use 0 based labels since the array is 0 based
let tabBarItem0 = tabBar.items![0] as! UITabBarItem
let tabBarItem1 = tabBar.items![1] as! UITabBarItem
let tabBarItem2 = tabBar.items![2] as! UITabBarItem
let tabBarItem3 = tabBar.items![3] as! UITabBarItem
// The UIColor method you are using is an initializer in swift
tabBar.barTintColor = UIColor(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)
// Using Assets with the various sizes loaded (1x, 2x, 3x) is better.
tabBarItem0.selectedImage = UIImage(named: "815-car-selected")
tabBarItem1.selectedImage = UIImage(named: "742-wrench-selected")
tabBarItem2.selectedImage = UIImage(named: "710-folder-selected")
tabBarItem3.selectedImage = UIImage(named: "724-info-selected")
return true
}
I recommend just looking at the documentation in XCode. All documentation is written in Swift and Objective C so it is very easy to translate between the two languages. Also read apple's swift basics to understand this code translation better: https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_467
Translation:
// Type casting in swift is "as Type"
tabBarController = self.window.rootViewController as UITabBarController
tabBar = tabBarController.tabBar
// Retrieving array values at indices can be shortened as array[index]
tabBarItem1 = tabBar.items[0] as UITabBarItem
tabBarItem2 = tabBar.items[1] as UITabBarItem
tabBarItem3 = tabBar.items[2] as UITabBarItem
tabBarItem4 = tabBar.items[3] as UITabBarItem
// The UIColor method you are using is an initializer in swift
tabBar.barTintColor = UIColor(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)
// UIImage also has an initializer for your situation in swift
tabBarItem1.selectedImage = UIImage(named: "815-car-selected#2x.png")
tabBarItem2.selectedImage = UIImage(named: "742-wrench-selected#2x.png")
tabBarItem3.selectedImage = UIImage(named: "710-folder-selected#2x.png")
tabBarItem4.selectedImage = UIImage(named: "724-info-selected#2x.png")
Maybe you should use image and selected image.
So have 2 different images use one for normal and selected
tabBarItem.image = UIImage(named:"ImageName")
and
tabBarItem.selectedImage = UIImage(named:"ImageName")
That way when the tab is selected it will use selectedImage. If it's not selected it will use the normal image.
Am newbie to ios and I found this solution on making the UINavigationBar Transparent.
Where in my project files can I put this code
[self.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;
So that it is applied in my entire project where navigation controller is being used.
Put in your viewDidLoad function of your rootViewController this code:
Objective-C:
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
Swift 2.x:
if let navigationBar = navigationController?.navigationBar {
navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
navigationBar.shadowImage = UIImage()
navigationBar.translucent = true
navigationController?.view.backgroundColor = .clearColor()
}
Swift 3:
if let navigationBar = navigationController?.navigationBar {
navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationBar.shadowImage = UIImage()
navigationBar.isTranslucent = true
navigationController?.view?.backgroundColor = .clear
}
This works for sure! Transparent UINavigationBar.
If you want change the appearance for all your application, I recommend you to use this:
[[UINavigationBar appearance] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
hope that will help
In your UIViewController class. You can also use UIAppearance mechanism http://nshipster.com/uiappearance/
And place this
[[UINavigationBar appearance] setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
[UINavigationBar appearance].shadowImage = [UIImage imageNamed:#"Your image file here"];
into
- ( BOOL ) application:( UIApplication* ) application didFinishLaunchingWithOptions:( NSDictionary* ) launchOptions
Transparent UIToolbar:
self.toolbar.setBackgroundImage(UIImage(),
forToolbarPosition: UIBarPosition.Any,
barMetrics: UIBarMetrics.Default)
self.toolbar.setShadowImage(UIImage(),
forToolbarPosition: UIBarPosition.Any)
Transparent UINavigationBar:
self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.translucent = true
set following code
Self.navigationcontroller.navigationbar.transculant=yes;