Prevent UiNavigationBar Title from getting Cut off? - ios

I am trying to customize the appearance of the navigationBar title in my ios application. This is the code I currently have:
NSMutableDictionary *navigationTitleAttributes = [NSMutableDictionary dictionary];
[navigationTitleAttributes setValue:[UIColor whiteColor] forKey:UITextAttributeTextColor];
[navigationTitleAttributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor];
[navigationTitleAttributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
[navigationTitleAttributes setValue:[UIFont fontWithName:#"Calibri" size:30] forKey:UITextAttributeFont];
[[UINavigationBar appearance] setTitleTextAttributes:navigationTitleAttributes];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-8 forBarMetrics:UIBarMetricsDefault];
The code yields the following effect:
It works great but my title gets cut off from the bottom.
I've seen solutions to this problem that use a custom UIView (such as this one: UINavigationbar title is cut off when using titleTextAttributes). However, that particular solution requires that the titleView property of the navigation bar be updated for each screen.
I was wondering if there was a simple solution that would cascade through my entire application.
Thanks

Th simple solution is to not use such a large font size. If you set the size to zero then the text should be auto-sized as appropriate.
Otherwise, using a custom view is the correct solution. You can subclass the navigation controller or navigation bar in order to ensure that all of the views have the label styled in the same way.

If you're using a custom font, you may be having the same problem I was. I found a few answers on this post to be quite helpful. I changed my descender values in my .otf font file to prevent my font from being cut off on the bottom. It was especially prevalent in iOS 7.
Custom installed font not displayed correctly in UILabel

Related

iOS Navigation Bar Height

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.

Weird difference between colours - UINavigationBar and UISearchBar

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.

appearance proxy not working as intended for UIButton font

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/

Changing appearance of UIBarButtonItem also changes position of navigationbar and toolbar?

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?

How to change UITabBar Selection color

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.

Resources