Change UI Tab bar Display color - iphone

I am new to ios. I want make app in tab bar
The tab bar colors now in this scren shot.
An the UI i want in my iphone is this image
IN each of icon of tab bar i used black images
Thanks in Advance

In your didFinishLaunchingWithOptions method in the app delegate you will need to add some code like this.
//Get the Tab Bar
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.delegate = self;
UITabBar *tabBar = tabBarController.tabBar;
//You may want to set the background of the tab bar (optional)
[tabBar setBackgroundImage:[UIImage imageNamed:#"CustomTabBar.png"]];
//You will need to repeat this code for each tab bar item
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
tabBarItem3.title = #"Settings";
tabBarItem3.image = nil;
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:#"settings-button-selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"settings-button.png"]];
Don't forget to make #2x images for retina support.
You can also change other settings for each tabBarItem - check out the docs to find out more.

You have to set each of your UITabBarItems separately using something like:
UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:#"Title1" image:[UIImage imageNamed:#"image1.png"] tag:1];
[tabBarItem1 setFinishedSelectedImage:nil withFinishedUnselectedImage:[UIImage imageNamed:#"image1.png"]];
[[myTabBarController.viewControllers objectAtIndex:0] setTabBarItem:tabBarItem1];
for each of your view controllers.

Related

UITabBarController selected item

I have UITabBarController and push on two views. I want the first view to be the default and the button to be in the "selected" state when it loads.
My problem is the tabBarItem does not have the selected background when it loads. I have to tap on it to show the selected. I'm also using custom images for the tab bar items as well and have them being configured in each view that gets added to the tabBarController.
I'm using this to set the selected item:
_tabBarController.selectedViewController = [_tabBarController.viewControllers objectAtIndex:0];
The UITabBarItem gets created in each view that I add to the tabBarController:
-(UITabBarItem *)tabBarItem
{
return [[UITabBarItem alloc] initWithTitle:#"" image:[UIImage imageNamed:#"tab_select_indicator_map.png"] tag:0];
}
How can I also have the tabBarItem show the selected state?
Use following code of display selected tabBarItem when your tabBar is loaded.
[_tabBarController setSelectedIndex:0]; // your can set index as per your requirement.
Eited:
Make sure that you created your tabBarItem such like
UITabBarItem* myTestingItem = [[UITabBarItem alloc] init];
[myTestingItem setFinishedSelectedImage: [UIImage imageNamed: #"btnTabItem-SelectedImage.png"]
withFinishedUnselectedImage: [UIImage imageNamed: #"btnTabItem.png"]];

Only image as UITabBarItem

I would only like to have an icon as the UITabBarItem and not the text underneath and I was wondering if this was possible, if so, how? TIA
REMOVE THE TITLE
The easy way with inspector
Select your TabbarItem
In Title Position change to Custom Offset
Set vertical to 100
Done
Fix IMAGE
Adjuste the image
Select your TabbarItem
define your Top and Bottom
If you use story board, maybe you can use Image Inset from size inspector of bar item and set title to empty at the same time:
if you just set the image and pass nil to title, the image will be displayed at the top level of the UITabBar item. You have to set the position also.
int offset = 7;
UIEdgeInsets imageInset = UIEdgeInsetsMake(offset, 0, -offset, 0);
After you set icon image for TabBarItem, you set the property value to change the image position, doing:
uiViewController.tabBarItem.imageInsets = imageInset;
Setting the item's title to nil is often insufficient, because if you set the view controller's title it will set the title of the tabbar item also.
Instead, do this:
tabbarItem.titlePositionAdjustment = UIOffsetMake(0.f, 50.f);
You can use this Swift extension to define a new method tabBarItemShowingOnlyImage(), which will return any UITabBarItem modified to show only the image:
// helper for creating an image-only UITabBarItem
extension UITabBarItem {
func tabBarItemShowingOnlyImage() -> UITabBarItem {
// offset to center
self.imageInsets = UIEdgeInsets(top:6,left:0,bottom:-6,right:0)
// displace to hide
self.setTitlePositionAdjustment(UIOffset(horizontal:0,vertical:30000))
return self
}
}
This extension builds from the advice offered in other comments.
It hides the title by displacing it, rather than setting it to nil, because sometimes other objects like the view controller itself will set the title to some value after the tab bar item is initialized. It centers the image by using the imageInsets to offset it by 6pt, a value I get just from eyeballing it on an iPhone 6 running iOS8.3.
I imagine that other devices and layout configurations might require a different offset correction, so a general solution would probably have to be more complex.
I know its been quite some time. But I might have a solution for this problem.
#ThiagoPires answer is useful only if the image is big enough to cover the title. In case if it isn't, then the title will be visible under the image.
I think that the easiest way to accomplish this, is by setting the title of the tabBarItem to "" in your view controllers code.
It is important to know that every time you change your view controllers title, the tabbaritem's title will be updated too. You could go fancy using KVO to observe the title's change and set "" to the tabbaritem title accordingly. But if you don't change it in many places in your code, you could just set it right in the place you change the ViewController's title.
Here is an example in swift:
override func viewDidLoad() {
super.viewDidLoad()
self.title = "View controller's Title"
self.tabBarItem.title = ""
}
That's it. I hope that this could help.
Relevant docs (highlighting mine):
initWithTitle:image:tag:
Creates and returns a new item using the specified properties.
- (id)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag
Parameters
title: The item’s title. If nil, a title is not displayed.
image: The item’s image. If nil, an image is not displayed.
The images displayed on the tab bar are derived from this image. If this image is too large to fit on the tab bar, it is clipped to fit. The size of a tab bar image is typically 30 x 30 points. The alpha values in the source image are used to create the unselected and selected images—opaque values are ignored.
tag: The receiver’s tag, an integer that you can use to identify bar item objects in your application.
Return Value: Newly initialized item with the specified properties.
Try with below code:
UIViewController *viewController1=[[RootViewController alloc]initWithNibName:#"RootViewController" bundle:nil];
UIViewController *viewController2=[[SecondViewCtr alloc]initWithNibName:#"SecondViewCtr" bundle:nil];
UIViewController *viewController3=[[ThirdViewCtr alloc]initWithNibName:#"ThirdViewCtr" bundle:nil];
UIViewController *viewController4=[[FourthVIewCtr alloc]initWithNibName:#"FourthVIewCtr" bundle:nil];
UIViewController *viewController5=[[FIfthViewCtr alloc]initWithNibName:#"FIfthViewCtr" bundle:nil];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:viewController4];
UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:viewController5];
peekArray = [[NSMutableArray alloc] init];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1, nav2,nav3,nav4,nav5, nil];
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];
UITabBarItem *tabBarItem5 = [tabBar.items objectAtIndex:4];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"home123.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"home_112.png"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"gift123.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"gift_112.png"]];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:#"cam12.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"cam_112.png"]];
[tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:#"message12.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"message_112.png"]];
[tabBarItem5 setFinishedSelectedImage:[UIImage imageNamed:#"people12.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"people_112.png"]];
[self.tabBarController setSelectedIndex:0];
self.window.rootViewController = self.tabBarController;
like this
UITabBarItem * personalBarItem = [[UITabBarItem alloc] init];
[personalBarItem setFinishedSelectedImage:[UIImage imageNamed:#"personal_click"] withFinishedUnselectedImage:[UIImage imageNamed:#"personal"]];

How can I place a header image fixed at the top of my Tab Application?

I want to create a Tab Application with a header image that is always present, no matter what Tab Item is active.
The example would be Foursquare:
I want to be able to place buttons and have different information displayed on that header.
Is that a simple Navigation Bar or something else?
Usually, for each tab is associated a viewController. You can notice it in the boilerplate which xcode creates when you choose "Tabbed Application".
Then, in each viewDidLoad or in the init of each viewcontroller you can set:
self.navigationItem.titleView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.png"]];
Then just change self.navigationItem.leftBarButtonItem and self.navigationItem.rightBarButtonItem with your controls on each viewController.
Edit:
In the appDelegate (in the didFinishLaunchingWithOptions method) you've to set something like this if you want use the navigationcontrollers:
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *myNav1=[[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController *myNav2=[[UINavigationController alloc] initWithRootViewController:viewController2];
UIImage *navBackgroundImg = [UIImage imageNamed:#"bg_navBar.png"];
UIImage *tabBackgroundImg = [UIImage imageNamed:#"bg_tabBar.png"];
[myNav1.navigationBar setBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault];//iOS 5 only
[myNav2.navigationBar setBackgroundImage:navBackgroundImg forBarMetrics:UIBarMetricsDefault];//iOS 5 only
[[UITabBar appearance] setBackgroundImage:tabBackgroundImg];//iOS 5 only
self.tabBarController.viewControllers = [NSArray arrayWithObjects:myNav1, myNav2, nil];
self.window.rootViewController = self.tabBarController;
Looks like a simple navigation bar, but they aren't exactly simple. You need to place/create a NavigationItem on the bar (after placing/creating the bar itself) and then set the titleView to a custom view with your image. According to the documentation the left bar button (close in your first screen) has to be nil or else the titleView is ignored. Though you can place buttons in this custom view for left buttons.
I found out the easiest thing to do is to write:
- (void)viewDidLoad
{
[[UINavigationBar appearance] setBackgroundImage:
[UIImage imageNamed:#"UINavigationBar.png"] forBarMetrics:UIBarMetricsDefault];
}
At my MainViewController, and all Navigation bars that get created on each view controller gets configured like that.

How to scale a TabBarItem Image

I want to add a UITabBar in my UIViewController, I don't want to use UITabBarController because I need to push this view controller into a navigation controller.
Everything is fine except that my images for UITabBarItem is not scale to fit the item size to be displayed properly.
How to fix this problem?
Here is some code:
UITabBar *myTabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0.0, barHeight, screenBounds.size.width, 50.0)];
myTabBar.opaque = YES;
UITabBarItem *barItem1 = [[UITabBarItem alloc] initWithTitle:#"title1" image:[UIImage imageNamed:#"icon1.png"] tag:1];
UITabBarItem *barItem2 = [[UITabBarItem alloc] initWithTitle:#"title2" image:[UIImage imageNamed:#"icon2.png"] tag:2];
UITabBarItem *barItem3 = [[UITabBarItem alloc] initWithTitle:#"title3" image:[UIImage imageNamed:#"icon3.png"] tag:3];
UITabBarItem *barItem4 = [[UITabBarItem alloc] initWithTitle:#"title4" image:[UIImage imageNamed:#"icon4.png"] tag:4];
NSArray *tbItems = [NSArray arrayWithObjects:barItem1, barItem2, barItem3, barItem4, nil];
myTabBar.items = tbItems;
You should save your TabBar images as 42x42. Then, in the name of the file ad the #2x directive.
i.e. icon1#2x.png
We do this so that the icon is loaded at 42x42 pixels on the iPhone 4/iPod Touch 4G, but scaled down to 21x21 for older devices.
This is going to save you a lot of time. Also note that the UITabBar only cares about the alpha channel of the image. So single color images are a good idea. This will save space.
I feel that you should be using a UIToolBar or UISegmentedControl and not a UITabBar.
UITabBar's should usually be used with a UITabBarController to manage it and should be used to switch app "mode" as Apple puts it. This means the TabBarController should be the rootViewController (an exception would be if you add a login view before the app starts up as a tab bar app).
this thread might also help

Add Custom buttons to Navigation Controller

I'm trying to either add 3 custom buttons to my navigation controller toolbar on the top of my view or add a segmented control with 3 options. I have the following code on my app delegate for when i create my view controller(fwc) but the buttons dont appear.
/*
Set up the navigation controller for the Feeding Tab
*/
// instantiate the feedingViewController and set the title to Feedings
feedingViewController *fwc =
[[feedingViewController alloc] initWithNibName:#"feedingViewController"
bundle:[NSBundle mainBundle]];
//fwc.title = #"Feedings";
// set the tab bar item up and add it as feedingViewController's tab bar item
UITabBarItem *feedingTabBarItem =
[[UITabBarItem alloc] initWithTitle:#"Feedings" image:nil tag:0];
fwc.tabBarItem = feedingTabBarItem;
[feedingTabBarItem release];
// create a new nav controller for feedings and add root view
feedingNavController = [[UINavigationController alloc] init];
//Create the add button, need to change the selector to something though *****
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(newFeeding)];
//self.navigationItem.rightBarButtonItem = add;
UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
// Create and configure the segmented control
UISegmentedControl *sortToggle = [[UISegmentedControl alloc]
initWithItems:[NSArray arrayWithObjects:#"Ascending",#"Descending", nil]];
sortToggle.segmentedControlStyle = UISegmentedControlStyleBar;
sortToggle.selectedSegmentIndex = 0;
[sortToggle addTarget:self action:#selector(toggleSorting:)forControlEvents:UIControlEventValueChanged];
// Create the bar button item for the segmented control
UIBarButtonItem *sortToggleButtonItem = [[UIBarButtonItem alloc]initWithCustomView:sortToggle];
[sortToggle release];
// Set our toolbar items
feedingNavController.toolbarItems = [NSArray arrayWithObjects:
flexibleSpaceButtonItem,
sortToggleButtonItem,
flexibleSpaceButtonItem,
add,
nil];
feedingNavController.navigationController.navigationBarHidden=NO;
[sortToggleButtonItem release];
[add release];
// Push the feedingViewController on the nav stack and release it.
[feedingNavController pushViewController:fwc animated:NO];
[fwc release];
In order to use a UITabBar you would need a UITabBarController, which is different than the UINavigationController. A UITabBar has a fundamentally different use than a UISegmentedControl. It appears that the functionality you're trying to implement is not appropriate for a UITabBar. In your question description you mention trying to add these buttons to the "navigation controller toolbar on the top." A UINavigationController has a UINavigationBar, which is the bar that runs across the top, and a UIToolbar, which is the bar that appears at the bottom. The UIToolbar, by default, is set to hidden, but you get a UIToolbar for free whenever you create a UINavigationController (see the UINavigationController reference in Xcode).
Apple's NavBar demo shows how to put a UISegmentedControl into the UINavigationBar. Instead of a title, use a custom titleView to display the segmented control:
fwc.navigationItem.titleView = sortToggle;
If you want to put your add UIBarButtonItem in the UINavigationBar as well, you can use:
fwc.navigationItem.rightBarButtonItem = add;
Note that you shouldn't actually go about trying to customize the UINavigationController's navigation bar on your own. The proper way to customize is to have an individual view controller access it's own navigationItem and set the titleView and rightBarButtonItem with the items you want.
If you wish to approach your problem using a UIToolBar instead, meaning that your items will appear on the bottom of the screen, you can do something like this:
// Assume UIBarButtonItem *add, UIBarButtonItem *sortToggleButtonItem,
// and UIBarButtonItem *flexibleSpaceButtonItem are allocated
[fwc setToolbarItems:[NSArray arrayWithObjects:
flexibleSpaceButtonItem,
sortToggleButtonItem,
flexibleSpaceButtonItem,
add,
nil]];
[feedingNavController setToolbarHidden:NO];