I have assigned a label to my navigation item.titleview
The problem is if I use system font then the label is centered on the navigation bar, but if I use custom font it is not.
There is a way to work around this without adding a custom label. By setting self.title from the viewDidLoad method, the titleLabel will resize. However, you need to use a different title then defined in your storyboard. It won't call sizeToFit when the contents of the label doesn't change (which sounds logical to me).
It is a known bug BUT you can circumvent it in the following way:
// adds a custom title label to the view
-(void)addNavBarTitle:(NSString*)titleText
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 360, 30)]; // NOTE: YOU CAN ADD PADDING TO THE LABEL BY CHANGING ITS Y ORIGIN
[label setFont:[UIFont fontWithName:#"Arial-BoldMT" size:14]]; // Use custom font if needed
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:titleText]; // Dynamically change title. Else just explicitly set the string here
label.textAlignment = UITextAlignmentCenter;
[self.navigationController.navigationBar.topItem setTitleView:label];
[label release];
}
That should do the trick,
Happy Coding!
This looks like a bug, so please file a report at https://bugreport.apple.com. Including the code from which you generated these screenshots would be extremely helpful.
In the meantime, you can try positioning the label yourself via the titleView property of the corresponding UINavigationItem.
The navigation bar centers the label based on its frame size. If you have made the label's frame larger than necessary or put extra whitespace after the visible text, it will appear off-center.
To make the label exactly as big as necessary for its text, send the sizeToFit message to the label. Or if you created the label in your nib, select the label and choose Editor > Size to Fit Content from the menu bar.
You could also set the label's textAlignment property to UITextAlignmentCenter.
Test project here: http://dl.dropbox.com/u/26919672/navbartitleview.zip
All above three answers are helpful ..
To sum up :
From the above answers.. i can too say it is a bug
#Dave Delong .. i tried updating its frame via titleView ..only origin.x and width had an effect..y values didn't.
#rob mayoff my first code include size to fit it didn't worked... then i tried many things.. making a button and setting its title.. etc.. still the bug persist.
#rinzler i tried that ..didn't worked for me.bug was still there with my font...might work for someone else..
So the solution was simple enough for me.. since my navigation bar title does not change.. i just replaced it with the text image.
using
UIImageView *TitleImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MyImage.png"]];
self.navigationItem.titleView = TitleImage;
[TitleImage release];
this won't work for those whose title changes.. Thanks all..
Assuming you're supporting iOS 5+, then you want to use [UINavigationBar appearance] method to set custom title fonts, bar button fonts, bar button images, etc.
Read more here:
http://www.raywenderlich.com/21703/user-interface-customization-in-ios-6
Related
I'm trying to add an image to a UINavigationBar that has a custom color. I was hoping to avoid having to use a .png for the entire thing, and to programmatically set the color, and drop the icon on top of the colored navigation bar.
self.navigationController.navigationBar.tintColor = [UIColor redColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"icon.png"] forBarMetrics:UIBarMetricsDefault];
The UIBarMetricsDefault causes a repeat for the image icon, and I just want one in the center. The UIBarMetricsDefault and UIBarMetricsLandscapePhone settings are not working for me, and I was hoping someone would know a better suggestion.
I am trying to avoid using a .png for the entire navigation bar, and would like to add the image to a color that is set programmatically. Thank you!
The way you set color is correct.
To set image in center use titleView property of navigationItem as shown below :
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon.png"]];
I have a problem regarding the UIButton title in iOS5. They are clipped and show like in this photo. I don't want them to be clipped, i want to show the full title.
In iOS6, they work perfectly.
Please tell me how can i resolve this?
you simply need to increase the width of your UIButton to show full Title, i have posted sample snippet for better idea, try it. You just need to increase the width of button.
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(20, 200, 150, 25)];
[btn1 setTitle:#"This is Long Title" forState:UIControlStateNormal];
[self.view addSubview:btn1];
Guys i have found the solution.
It was pretty simple, you just need to set the button font size in the interface builder to a larger or the same font size than that of the UIapperance font proxy.
I think AutoLayout may be the culprit for this. Check to see if your constraints aren't forcing the label to shrink.
I assume that you are setting your custom font from the code but you are setting the text for the buttons from the IB. So my simple explanation is that as #MichaelScaria said, your custom font is larger. The text is clipping because the size of the label inside the button is adjusted for the current font and size. Since you are changing the font and size after the text is set you need to re-set the title of the button or call 'sizeToFit' on that button.
For those of you here due to ellipsis and use of UIBarButtonItem know that there is a possibleTitles property on UIBarButtonItem with docs that read:
Use this property to provide a hint to the system on how to correctly size the bar button item to be wide enough to accommodate your widest title. Set the value of this property to an NSSet object containing all the titles you intend as possible titles for the bar button item. Use the actual text strings you intend to display.
This property applies to bar button items placed on navigation bars or toolbars.
I am trying to change the highlighted color of the tabBarItem just like the Viber app does. I don't want to change the color of all the Bar, just the highlighted color.
I have read a lot posts here, but none of them explained how to do only this modification.
Does someone know how to do this? There is many apps that have customized colors!
Currently, with iOS4, this type of customization is not possible with a standard UITabBar. Most applications that have customized UITabBars actually create their own. Check out CocoaControls for some drop in third-party solutions you could use.
Have you tried:
UIView *bg = [[[UIView alloc] initWithFrame:CGRectMake(0,0,320,49)] autorelease];
[bg setBackgroundColor:[UIColor redColor]];
[self.tabBarController.tabBar insertSubview:bg atIndex:1];
That code initially creates a background view for the tabBar and sets it's colour to red.
I have this code in my applicationDidFinishLaunching:
navController.toolbarHidden = NO;
[navController toolbar].tintColor = [UIColor darkGrayColor];
[[navController toolbar] setFrame:CGRectMake(0.0,0.0,320.0,180.0)];
The first two lines definitely have an effect on the UI. For example, if I set toolbarHidden to YES, it is certainly hidden. However, when I try to set the frame and customize the toolbar height, no change takes place. Does anyone know how to fix this?
UIToolbars have a fixed height. You won't be able to change it.
You have to make your own toolbar class if you need one with an adjustable height.
if you were to create your own toolbar, you can just use the frame property of the toolbar to create it. UINavigationController's toolbar height is not adjustable.
I have downloaded TableView sample code '4_TableViewCellSubviews' from internet. I am trying to run this program in my way. I am trying to set my own background view color as 'lightgraycolor'. I just wanted to see the background view as lightgraycolor in this application. I don't want to make any color for TableView. i.e I want to give gray color only for the background view, not for the background of table.
When I provide the code for that as '[self.view setBackgroundColor:[UIColor lightGrayColor]];' , it is changing the color for the entire table as well.
But I am trying to set Color only for the view which besides on the Table. How to set background color only for the background view and not for the TableView. I know that the code which I provide to set the color background view is perfect. What are the possibilities that we would get to occur with this issue and solution for this?
I have kept the screenshot at the following location:
http://www.quickfiles.net/894107
Update
I think most you all have misunderstood my problem. I want to have color on the space when we scroll DOWN the Table we see a empty space on the top in the background of tableview.
For example, download a free news application called, "BusinessWeek". You can launch this app in your iPhone device. You can see the home screen and scroll DOWN the table there. You can see the background color as Gray color of the Table. I want to do the same way, what should I do for that?
In this case here, the 'view' and 'tableView' are the same actually, that's the color change doesn't work as you want it to. To convince you of that, add these 2 lines in the 'viewDidLoad' function, and look at what is output in the console when you run your program:
NSLog(#"tableView: %#", self.tableView);
NSLog(#"view: %#", self.view);
Sorry, the earlier answer comment needs to modified as below steps. I am writing the solution here because some one else if they come across they can use it.
In I.B, Create UIView and set the color whatever you want. Create Table on top of it.
Create a TableView variable in your code and connect that with I.B Table.
In viewDidLoad, add like below.
myTableView.backgroundColor = [UIColor clearColor];
myTableView.alpha = 0.9;
[super viewDidLoad];
Add the below code in cellForRowAtIndexPath:
UIView *v = [[UIView alloc] initWithFrame:cell.frame];
v.backgroundColor = [UIColor whiteColor];
cell.backgroundView = v;
[v release];
// If you want accessory type add this below line ..
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Build and Run you application.
[tableView setBackgroundColor:[UIColor clearColor]];
You should add the tableView as a subview of the MainView. Create a ViewController class and programmatically add the tableview methods and delegate. At program start, add your tableview as a subView of the Mainview. In this way UITableView and UIView should be two distinct parts, and you should be able to change the background color properly.
My problem was also same whatever the below existing query is talking about 'Gutter' color change:
Set UITableViewCell background color on plain style table view?
Currently i have resolved it by the following code:
I have created a UIView and set the background color as 'Gray'. I created a TableView on top of it and connect with the tableView variable there.
When i run my app, it has shown the Gray color in the background view and also in Table. I don't want that color in Table, so added the below code as well to remove it.
in 'viewDidLoad'
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UIView *myView = [[UIView alloc] initWithFrame:cell.frame];
myView.backgroundColor = [UIColor whiteColor];
cell.backgroundView = myView;
[myView release];
cell.contentView.backgroundColor = [UIColor whiteColor];