Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
Is it possible to set the color of the top-title of a view, ie. the one appearing on top of everything ("Testing" on this image: http://pessoal.org/blog/wp-content/uploads/2009/02/standard-200x300.png)?

Ive tried:

Code:
self.title = [UIColor lightGrayColor];
// or
self.title.textColor = [UIColor lightGrayColor];

..but no success.

I know Im pointing to the correct title with "self.title" since:

Code:
self.title = @"Dude";

..does in fact change the title to "Dude".
 
What you're really changing here is the title property of a UINavigationItem. And since title is just an NSString, no, you can't change its color. You may be able to achieve what you want by placing a UILabel into the titleView and going from there.
 
What you're really changing here is the title property of a UINavigationItem. And since title is just an NSString, no, you can't change its color. You may be able to achieve what you want by placing a UILabel into the titleView and going from there.

yes, that works. like dejo said, you have the an UINavigationItem here. If you read through the reference of UINavigationItem
http://developer.apple.com/iphone/l...ionItem_Class/Reference/UINavigationItem.html
you will see that instead of the title you can set a titleView property, which can be any kind of UIView. With this, you can for example display an image as the title (UIImageView) or, that's what you want, an UILabel.
 
Ah, I see :)

I must be doing something wrong, though, since now Im not getting any title at all - not "Hello" and not the previous standard one I had without this code.

Code:
UILabel *myLabel = [[UILabel alloc] init];
myLabel.text = @"Hello";
myLabel.textColor = [UIColor lightGrayColor];
self.navigationItem.titleView = myLabel;
 
I must be doing something wrong, though, since now Im not getting any title at all - not "Hello" and not the previous standard one I had without this code.

Code:
UILabel *myLabel = [[UILabel alloc] init];
myLabel.text = @"Hello";
myLabel.textColor = [UIColor lightGrayColor];
self.navigationItem.titleView = myLabel;
You haven't set the frame for myLabel.

Also, be careful. titleView is ignored if leftBarButtonItem is not nil.
 
@Danneman101, give your label a frame and use initWithFrame. Yours has no size. You might also set the autoResizemask.
 
Just realize that you are going to have to do a bunch of formatting (font, backgroundColor, etc.) to your label to get it even close in appearance to the default title look.
 
Thanks guys for your help :)

Yes, a lot of formatting seems to be needed. The biggest problem seem to get the label from not moving in realtime, and hit the exact middle spot in the GCRectMake.

Code:
	// --------------------
	// SET:		Title-Label
	// --------------------
	UILabel *myLabel = [[UILabel alloc] init];
	myLabel.frame = CGRectMake(150, 0, 100, 44); //x,y,width,height	
	myLabel.textColor = [UIColor lightGrayColor];
	myLabel.backgroundColor = [UIColor clearColor];
	myLabel.text = self.title;
	self.navigationItem.titleView = myLabel;
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.