UITabBarItem title position - iphone

On searching the web on how to adjust the position of a UITabBarItem title position i ran over this similar post and still wondering how to do that.
Is it even possible to adjust the title position a bit from the bottom up? (for example 5px) I need this because i have custom images and now the position of the title is not perfect.

If you want to move up simply set vertical offset to negative value
UITabBarItem* it = [[self.tabController.tabBar items] objectAtIndex:0];
it.titlePositionAdjustment = UIOffsetMake(0.0, -2.0);
You don't have to use proxy as it is done here UITabBarItem title in centre of title, not at bottom.You can define offset per item.

Swift version for the lazy ones :)
UITabBarItem.appearance().titlePositionAdjustment = UIOffsetMake(0.0, -4.0)

If you want to update all of them:
tabBar.items?.forEach({ $0.titlePositionAdjustment = UIOffset(horizontal: 0.0, vertical: -2.0) })

Why don't you just have an empty title property for your view controller and add the title to your custom images for the tab?
You can do this (in iOS 5.0):
UIImage* iconSelected = [UIImage imageNamed:#"tabIconSelected.png"];
UIImage* iconNotSelected = [UIImage imageNamed:#"tabIconNotSelected.png"];
UITabBarItem *updatesListItem = [[UITabBarItem alloc] initWithTitle:#"" image:iconSelected tag:0];
[updatesListItem setFinishedSelectedImage:iconSelected withFinishedUnselectedImage:iconNotSelected];
[navigationController setTabBarItem:updatesListItem];
where tabIconSelected.png and tabIconNotSelected.png both contain the title text for the tab.
I have written a brief article "Add some colour to your UITabBar icons" which explains how to use custom images with tabs.
Hope this helps.

Global adjustment in objective-C:
[[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, -4)];

Related

UISegmentedControl inside title of UINavigationBar looks unformatted

I'm trying to add a UISegmentedControl within the title of a UINavigationController. However, the formatting looks like this (i.e. its ugly).
When I want it to look like this (pretty :). Can anyone help??
I've read the popular example by Red Artisan here. But I'm not showing this as my first view (like Red Artisan does), so I've moved a lot of the code out of App Delegate. In App Delegate, I do set up this screen to be a UINavigationController with its rootView a UIViewController.
GenInfoViewController *genInfoController = [[GenInfoViewController alloc] initWithNibName:#"GenInfoViewController" bundle:nil];
UINavigationController *genInfoNavController = [[UINavigationController alloc] initWithRootViewController:genInfoController];
Then in viewDidLoad of GenInfoViewController.m I do the following:
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:#[#"Info",#"Map"]];
self.navigationItem.titleView = self.segmentedControl;
To style a segmented control, set the segmentedControlStyle property to one of the following:
UISegmentedControlStylePlain
UISegmentedControlStyleBordered
UISegmentedControlStyleBar
UISegmentedControlStyleBezeled
For example:
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:#[#"Info",#"Map"]];
self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBordered;
self.navigationItem.titleView = self.segmentedControl;
    
There's some relevant Q+As on here regarding styling segment controls:
Custom segment control
Remove rounded corner
Change font size
Change colour of selected segment
If you'd like to try a custom segmented control, check out all the available CocoaControls and CocoaPods.
Yup, you need to set the property "segmentedControlStyle" on your UISegmented control.
Your options are as follows:
typedef enum {
UISegmentedControlStylePlain,
UISegmentedControlStyleBordered,
UISegmentedControlStyleBar, // This is probably the one you want!
UISegmentedControlStyleBezeled,
} UISegmentedControlStyle;
So the following should probably do the trick:
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:#[#"Info",#"Map"]];
self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
self.navigationItem.titleView = self.segmentedControl;
One thing you might also want to consider is setting the "tintColor" of the segmented control too.
self.segmentedControl = [UIColor blackColour];
Will leave you with something like this:
Obviously there is lots of other customisation you can do too. Take a look at the documentation here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UISegmentedControl_Class/Reference/UISegmentedControl.html

How to customize UITabBar text labels?

I have customized a Tab Bar using the methods from Ray Wenderlich and iOS Blog but because I'm using a light color as background the text labels look awful and I want to change them to match my style. Is it possible and how?
If its not possible or if its too complex I would like to ask how to move the icon image down so its centered on the Tab Bar? This way I could "write" the labels on the .png files.
I'm planning on developing this app for iOS5+ so there is no worry about making it compatible with iOS4 (although if anyone finds a easy way of doing this I would really appreciate).
Here's what I got so far and here's what I want.
The code for my tabBarBackground:
UIImage* tabBarBackground = [UIImage imageNamed:#"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"selection-tab.png"]];
And for the Icons:
//Custom TabBar Items
UIImage *selectedImage = [UIImage imageNamed:#"home-icon-selected.png"];
UIImage *unselectedImage = [UIImage imageNamed:#"home-icon.png"];
UITabBar *tabBar = rootTabBarController.tabBar;
//Icons for the 1st Tab
UITabBarItem *item1 = [tabBar.items objectAtIndex:0];
[item1 setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];
You can do this by accessing the tab-bar item object of the tabbar for each view controller.
Check out some documentation here for more examples.
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/CombiningViewControllers/CombiningViewControllers.html
Use this to customize UITabBarItem text:
[[UITabBarItem appearance] setTitleTextAttributes: aNSDictionaryOfTextAttributes];
with a dictionary of attributes similar to those shown in the answer here.

adding icon between back button & title of navigation bar

I want to put an icon by side of the title of my navigation bar. I'd prefer not to implement it as a custom titleView, because then i'll need to create a custom titleView for each controller i put on the stack (and I have pretty deep nesting). I'm adding currently as an UIImageView to a navigationBar. My problem is to calculate exactly this icon's horizontal position. It depends on the width of the back button, which has each time another title. How do I calculate this back button frame? Googling on it seems doesn't bring any reasonable results.
Thanks in advance
You could calculate the size of a label, with
your text in it and experiment with what the button will add on..
CGSize s = [label sizeWithFont:_font];
I ran into this same issue. My solution was to add the icon as one of the leftBarButtonItems, after a flexible space. This pushes it up against the title.
UIBarButtonItem *space = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIView *icon = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"titleIcon"]] autorelease];
UIBarButtonItem *iconItem = [[[UIBarButtonItem alloc] initWithCustomView:icon] autorelease];
NSArray *items = [NSArray arrayWithObjects: space, iconItem, nil];
self.navigationItem.leftBarButtonItems = items;
self.navigationItem.leftItemsSupplementBackButton = YES;
It's not quite the same as creating a custom titleView, however, in two ways: (1) the title text remains centered if possible, with the icon to its left, rather than centering the title and icon together; and (2) when you push a new view onto the stack, the icon does not slide to the left with the title; instead it fades out, like the other bar button items.
So, depending on your requirements, this solution may or may not work for you. But at least it avoids trying to match the standard title text attributes in a custom title view. (As far as I can discover, there is no automatic way to do that.)

UISegmentedControl in Mail app

How do I get a UISegmentedControl that is like the one in the Mail App, so that it is the same colour as UIToolbar buttons (as if both segments were in the selected state).
I want to use the segmented control for exactly the same purpose as Mail.
(on the iPad, so a grey not blue color)
This is code from Apple Sample codes... NavBar and both the images used in the code..
you shoud be able to get exact same view as mail App.
// "Segmented" control to the right
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"up.png"],
[UIImage imageNamed:#"down.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 90, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
defaultTintColor = [segmentedControl.tintColor retain]; // keep track of this for later
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
You seek the tintColor property!
When you use a UISegmentedControl you can change its tint color to any color you can dream up. So, if you added the UISegmentedControl in Interface Builder then you would style it in your - (void)viewWillAppear:(BOOL)animated method as such (assuming you had it hooked up to a #synthesized ivar:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Set the tintColor to match the navigation bar
self.mySegmentedControl.tintColor = [UIColor colorWithRed:.94 green:.94 blue:.94 alpha:1];
... do whatever else in your viewWillAppear ...
}
Now obviously you will want to play with the red, green, blue, and alpha's that I've put in the sample code above, but you can literally tint the UISegmentedController any color you would like (or make it as transparent as you would like), so it's just a matter of finding the RGBA values that look perfect to you.
Remember that per Apple's docs that the default value of this property is nil (no color). UISegmentedControl uses this property only if the style of the segmented control is UISegmentedControlStyleBar.
Good luck!
I dont know exactly what you mean.. but i believe the "UISegmentedControlStyleBar" as segmentedControlStyle could it be.
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar
You can set this property in the IB too! (It's the property called "style")
The style I'm looking for is undocumented: it is style 4.
It looks like he up/down control here: http://media.mobilemeandering.com/wp-content/uploads/2010/04/ipad-mail-message-2.png
(not my image btw)
It basically makes all segments look selected, it's intended for momentary pushes, and is effectively multiple tool bar buttons pushed up together.
So it can't be set in IB but must be set in code or manually in the nib/xib file, by opening the nib as a text file.
I'm not sure I exactly understand what you're trying to do, but I'll give it a shot.
The solution is not obvious, you need to use a UISearchDisplayController in order to get a matching UISearchBar and UISegmentedControl.
See the TableSearch sample code for an example.

iPhone SDK - UITabBarConroller and custom design

I am having a problem with my tab bars at the bottom of the screen. The designer has decided it should be one colour (not black) when inactive and another colour when active. I have worked out how to replace the main colour of the tabbar by subclassing UITabBarController and doing this:-
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0.0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
UIView *v = [[UIView alloc] initWithFrame:frame];
//get percentage values from digitalcolour meter and enter as decimals
v.backgroundColor = [UIColor colorWithRed:.0 green:.706 blue:.863 alpha:1];
[tabBar1 insertSubview:v atIndex:0];
[v release];
}
I just can't see how to make the active tabbar be a separate colour when it is selected. I have tried subclassing UITabBarItem but there doesn't seem to be any property for me to set to change the background colour of the tab.
They also want to have the icons on the tab bar not be blue and grey and I can't figure out how to do that. In the ViewController for one tab bar item I have put this into viewdidload:-
myTabBarItem *tabItem = [[myTabBarItem alloc] initWithTitle:#"listOOO" image:[UIImage imageNamed:#"starcopy.png"] tag:1];
tabItem.customHighlightedImage=[UIImage imageNamed:#"starcopy.png"];
self.tabBarItem=tabItem;
[tabItem release];
tabItem=nil;
and in my subclass of UITabBarItem I have put this:-
-(UIImage *) selectedImage{
return self.customHighlightedImage;
}
Only I don't see the icon at all.
If I put this into the viewDidLoad of my subclass of UITabBarController:-
for (UITabBarItem *item in tabBar1.items){
item.image = [UIImage imageNamed:#"starcopy.png"];
}
Then all my tab bars have the icon but they are blue (and grey when inactive) how would I get them not to become blue but stay their original colour?
If you have any light on this problem please help as I have been banking my head on the wall for 2 days now and it's getting me down.
Thanks in advance
Cheryl
It seems to be impossible to be done without using private API calls. The only solution I can see right now is to create a complete custom TabBar. That however is a lot of work just to get an icon presented in a different color.