novice iOS developer and I am trying to figure out why my Navigation bar height wont change after I programmatically change it. I have done the following things:
I added this piece of code to the AppDelegate.m in the didFinishLaunchingWithOptions method
[[UINavigationBar appearance] setFrame:CGRectMake(0, 0, 320, 200)];
I was told that this would create a much larger navigation bar but it doesn't seem to be doing so.
I have also added the following code in a specific view controller (FirstViewController.m) to change the font family, text color, etc.
-(void)awakeFromNib {
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:(72/255.0) green:(167/255.0) blue:(192/255.0) alpha:1]];
[self.navigationController.navigationBar setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"CaviarDreams" size:28], NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName, nil]];}
Am I doing something wrong here?
Any help would be great!!
You can change the height of a navigation bar if it is your navigation bar - if it's just a free-standing interface object.
But if you're using a UINavigationController, the height of the navigation bar is really not up to you. The UINavigationController does the layout of views, and you can't really change what it does. That is part of the price of using this built-in structure, if you see what I mean.
As for your other attempts, you are probably doing them at the wrong time. For example, in awakeFromNib it makes no sense to talk about self.navigationController.navigationBar as it is probably nil anyway at that time (you can easily check with logging). That is why it is better to use the appearance proxy. But then you are doing that too late; you have to use the appearance proxy in application:didFinishLaunching...:, because it affects only future instances of that type.
Basically as a newbie you need to learn what Cocoa lets you do and what it doesn't, and when are the right moments in the process to do those things. It's a big framework. You're in bed with a gorilla; you need to know when the gorilla wants to turn over and let it turn over, or you'll just get squashed. You'll get better at this as you become more accustomed to it.
i want to modify my code to change colour of UINavigationBar and UISearchBar.
For some odd reason, the same instance of UIColor provide different results. Please take a look at screen:
There is the code for customise appearance:
UIColor *color = [UIColor colorWithR:88 G:123 B:139 A:1];
[[UINavigationBar appearance]setBarTintColor:color];
[[UISearchBar appearance] setBarTintColor:color];
ColorWithR is a simple category that simplify process of getting colour using RGB. As you can see, i have same colour but if you look at screen, it different. How could this happen?
PS. Im sorry for this extremely large screenshot, i was unable to figure out how to make it small.
im currently styling my app via the appearance proxy and i ran into this problem:
when i set properties on the UIButton appearance my font is ignored:
[buttonAppearance setTitleColor:darkColor forState:UIControlStateNormal];
[buttonAppearance.titleLabel setFont:[UIFont fontWithName:#"Helvetica Neue" size:10.0]];
the first line is applied properly (darkColor is some UIColor), but my font change is ignored completely.
When i copy the line into my ViewController and apply it to a concrete button it works fine.
Am i missing something?
any help appreciated! ty
The font name is wrong, it should be HelveticaNeue, without the space between.
In the future if you want to see other iOS font names you should check this website piece of code
EDIT
After a closer look I realized that you are trying to set the appearance of the button's title which is a UILabel, sadly UILabel doesn't have the font property in the UIAppearance proxy and that's why the font doesn't work.
I have found this class TWTButton.h, who resolved my problems adding a new appearance selector [setTitleFont:] to the UIButton class.
buttonAppearance = [TWTButton appearance];
[buttonAppearance setTitleFont:[UIFont systemFontOfSize:10.0f]];
You may read more about this here : http://toastmo.com/blog/2013/01/17/uiappearance/
once again I have a problem with my custom navigation- and toolbar:
I've customised their tintColor and the font by using the appearance proxy in didFinishLaunchingWithOptions. Initially the bars should be hidden by setting their center outside the visible area in viewDidLoad. By single-tapping I use my own animation to slide the bars in/out. Everything was working just fine until the next step:
I wanted the UIBarButtonItems to have the same font like i used in the bars, so I went back to didFinishLaunchingWithOptions and added the following code:
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:FONT_HEADER size:0.0],UITextAttributeFont,nil] forState:UIControlStateNormal];
It works just fine for the font part, but somehow it also sets the bars back into the visible position before viewWillAppear so the bars are not initially hidden anymore. I tried to find out what causes this unmeant repositioning and couldn't find any connection. I also tried to reset the position at some later point like viewWillAppear but this somehow doesn't work for the toolbar.
EDIT: The described behaviour does only occur on devices with iOS 5 though. On the iOS6 simulator everything still seems fine.
Does anyone have a hint for me what is going wrong here or how I could smoothly solve this problem?
Thanks in advance
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:FONT_HEADER size:0.0],UITextAttributeFont,nil] forState:UIControlStateNormal];
what's the FONT_HEADER here? Did you check if the ios 5 support this font?
I need to change the selection color of UITabBar from default blue to red. How do we do this.
Update September 2017:
It's been two years since I've written this answer and since it's receiving upvotes regularly, I should say this is probably the worst possible answer to this question, it's error prone, likely to break because of iOS updates, hard to debug, etc., so please don't do the things I've written and apply better solutions such as subclassing UITabBar or UITabBarController. Thanks.
You can do this by setting a "tintColor" attribute (Key Path) for you UITabBar.
Select the UITabBar in the document outline. (NOT the Controller with the yellow icon.)
Select Identity Inspector in the Utilities area.
Click the + in "User Defined Runtime Attributes."
Add a "tintColor" Key Path of type "Color" and the color you want.
This should do it. You can check it against the screenshot below.
More on this:
There's a "Tint" attribute in Identity Inspector of UITabBar which I believed would do the exact same thing but apparently, it does nothing. It's default value is the exact default fill color when a UITabBarItem is selected, so my guess is it would be fixed in the stable release Xcode 7. Fingers crossed.
In IOS5, UITabBar has a selectedImageTintColor property which does what you need.
In iOS 7 it's simply the tintColor. One way to accomplish this could be to subclass UITabBarViewController, set the custom class in the storyboard, and in your viewDidLoad method of the subclassed tabBarVC add this:
[[self tabBar] setTintColor:[UIColor redColor]];
To achieve above result perform following steps.
Step 1: Add your desired images in Assets.xcassets, and make sure they Render As: Default
Step 2: Select your UITabBar object and set Image Tint color, this color will be selected tab color
Step 3: Select UITabBar object and add Key Path: unselectedItemTintColor, Type: Color, Value: Choose color for unselected item in User Defined Runtime Attributes.
All done.
It is extremely easy
Create a custom class of UITabBarController and in -(void)viewDidLoad method add this line:
[[self tabBar] setSelectedImageTintColor:[UIColor greenColor]];
Because UITextAttributeTextColor is deprecated in iOS 7, you should use:
[UITabBarItem.appearance setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor greenColor]} forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor purpleColor]} forState:UIControlStateSelected];
Starting from iOS 8 it's as simple as:
UITabBar.appearance().tintColor = UIColor.redColor()
Simply change the following property in Interface Builder for the TabBar
Obviously in my case its White.
The SDK does not make this easy, but it is technically possible. Apple apparently believes this to be part of their vision of a consistent look and feel.
UITabBar is a subclass of UIView. You can always subclass and implement your own -drawRect:
This is not a trivial task, however, you have to essentially re-implement the class from scratch or you risk some weird side-effects.
Swift 5 Programatically
It is pretty easy in Swift 5.
In your TabBarController write this:
tintColor = UIColor.red
That's it
I've been searching for a way to set the selected text color of a UITabBarItem and have found a dead simple method, using the UIAppearance protocol.
[UITabBarItem.appearance setTitleTextAttributes:#{
UITextAttributeTextColor : [UIColor greenColor] } forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:#{
UITextAttributeTextColor : [UIColor purpleColor] } forState:UIControlStateSelected];
Please excuse the awful colors!
iOS 5.0 fixes this issue but the solution is under NDA. Look up UITabBar in your documentation for an EASY way to do what you want to do.
I found the easiest solution -
Select Tab Bar in Tab Bar Controller
Set Image Tint color
Set Tint Color
For reference see the attached image.