iOS Navigation Bar Height - ios

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.

Related

Defining colours programmatically and using them in Interface Builder

I want to customize the colours of an App reusing the same elements but changing the colours.
These elements are simple Views, UIButton, UILabel, etc. nothing fancy. The colours may come from a XML or plist I will preload and parse to UIColor.
Yes, I can create an outlet from each of them and set them manually, but I got hundreds of elements and I want to avoid this path.
I tried IBInspectable, without luck, I’m looking for a widespread solution, all views, all VCs.
I am coding in Objective-C by the way...
Could you suggest any approach on how should I do this?
Comment if you want more detailing…
Thank you all very much!
Couple options...
Subclass your UI elements, and use MYUIButton instead of UIButton, for example, or
Look at UIAppearance proxy - https://developer.apple.com/reference/uikit/uiappearance - Using that, you can set default appearance characteristics for the whole app.
Example:
[[UIButton appearance] setBackgroundColor:[UIColor yellowColor]];
[[UILabel appearance] setBackgroundColor:[UIColor orangeColor]];
If you include those two lines (often done in AppDelegate / didFinishLaunchingWithOptions), every UIButton in your app will have a yellow background, and every UILabel will have an orange background.
(However, you will not see these changes in Interface Builder)
Edit:
As Charles mentions in his comment, you can create subclasses and then apply appearance changes to only those classes.
Suppose you have 3 button "types" that you want to apply a "color scheme" to - dark-blue, medium-blue, light-blue or dark-red, medium-red, light-red, etc. You could take the approach of creating DarkUIButton, MediumUIButton and LightUIButton subclasses. Then, when you load your color scheme...
[[DarkUIButton appearance] setBackgroundColor:scheme.darkcolor];
[[MediumUIButton appearance] setBackgroundColor:scheme.mediumcolor];
[[LightUIButton appearance] setBackgroundColor:scheme.lightcolor];

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.

Prevent UiNavigationBar Title from getting Cut off?

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

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