Center navigation bar title vertically - ios

I have a simple storyboard containing a navigation controller with a static table view controller. I have added a view controller and connected it to one of the cells via a push segue. One thing I have noticed is that the navigation bar title doesn't center properly vertically with the back button and UIBarButtonItem.
Here is an image of what it looks now.
Notice that it is all aligned to the bottom. What I want is, is a way to either move the title down or move the buttons up. If the buttons were moved up, the back button arrow should be moved as well, and allow to return to the main table view.
Here is the code I'm using to set the UIBarButtonItem font in the view controller (If it helps) :
[clearButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"ChalkboardSE-Regular" size:15], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
And here is what I'm using to set the font for the back button in the app delegate, didFinishLaunchingWithOptions::
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:#"ChalkboardSE-Regular" size:15]}forState:UIControlStateNormal];

You can move the title down using: [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:(float) forBarMetrics:UIBarMetricsDefault];

SWIFT version for iOS 8+
self.navigationController!.navigationBar.setTitleVerticalPositionAdjustment(adj, forBarMetrics: .Default)
self is a UIViewController:
Note: Consider specifying adjustments for the other bar metrics options, to refine the offset for landscape mode and compact devices, because one offset may not look right on all phones in all positions.

To move title down :
self.navigationController!.navigationBar.setTitleVerticalPositionAdjustmen(+Value, forBarMetrics: .Default)
To move title up:
self.navigationController!.navigationBar.setTitleVerticalPositionAdjustmen(-Value, forBarMetrics: .Default)

In swift 5:
self.navigationBar.setTitleVerticalPositionAdjustment(ajustment, for:.default)

Related

Where should this tab bar controller be embedded?

The Initial View Controller of this project is a Navigation Controller. I want to show a Tab Bar Controller on a View Controller later in the view hierarchy after the user signs in.
When I embed a Tab Bar Controller on the View Controller I want it to be on, the navigationBar I have programmatically implemented gets overridden with a new navigationBar item that, when pressed, leads the user back to the log in page.
Here is the code for the navigationBar.
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//Changes The Title Color
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName]];
//Sets The Navigation Controller Font
[self.navigationController.navigationBar setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIFont fontWithName:#"ArialMT" size:12.0], NSFontAttributeName,nil]];
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x0080ff)];
self.navigationController.navigationBar.translucent = YES;
self.navigationItem.leftBarButtonItem = btnRefresh;
//
self.navigationItem.rightBarButtonItem = btnCompose;
Here is how the navigationBar is supposed to be implemented on the View Controller:
Here is what happens when a Tab Bar Controller in embedded in the View Controller that I want it to be on:
What is the correct way to show a Tab Bar Controller on this View Controller that will preserve the existing navigationBar? Apple's Documentation on Tab Bar Controllers seems extensive and it seems it should be implemented programmatically.
According to Apple docs TabBarController should not be pushed to UINavigationController. Instead you can just use UITabBar and UITabBarDelegate to get same effect. You can refer below link of documentation.
https://developer.apple.com/documentation/uikit/uinavigationcontroller/1621887-pushviewcontroller

UIBarButtonItem loses custom appearance attributes when UIAlertController is presented

I have a UIBarButtonItem in a UINavigationBar that has some custom appearance attributes (custom font, color). It appears normally most places throughout the app.
However, when a UIAlertController with the ActionSheet style is presented over the view controller with this button, the BarButton loses its custom font appearance, leading to a strange looking "jump" on the button and an incorrect font being displayed, as can be seen here: http://giphy.com/gifs/puopiq9mPyI2Q/html5 .
I assume this is likely related to how we are setting the BarButton appearance, with the following code in AppDelegate:
NSDictionary *btnAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName,
[AppearanceConstants mediumFontSize20], NSFontAttributeName,nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:btnAttributes forState:UIControlStateNormal];
My guess would be that the bar button is not in UIControlStateNormal, however it doesn't seem like theres anyway for me to tell what state the BarButton is in when the AlertController is presented, and just trying to set the title text attributes to the appropriate values for every control state didn't work either.
When I navigate to other view controllers with BarButtonItems in their NavigationBars after doing this, they still have the correct appearance and if I go back to the problematic screen afterward it will have the correct appearance, until the AlertController is presented again.
I reset the style after presenting the ActionSheet
UIAlertController *alertVC = ...
[self presentViewController:alertVC animated:YES completion:^{
[self.navigationItem.leftBarButtonItem setDefaultAppearanceForSpecificItem];
[self.navigationItem.rightBarButtonItem setDefaultAppearanceForSpecificItem];
}];
I created a category on UIBarButtonItem to help me
[self setTitleTextAttributes:#{
NSFontAttributeName: [UIFont systemFontOfSize:14],
NSForegroundColorAttributeName: [UIColor VKBlueColor]
}
forState:UIControlStateNormal];

How to change a custom NavigationItem title and background color?

Earlier I used a navigationcontroller with this code :
self.navigationItem.title = #"News";
Now I have the problem that I no longer needed the navigationcontroller as I am using a page controller for navigation. Now I added a navigationbar however it doesn't change the title with this code anymore.
Also how can I change the background color?
IN iOS 7 use write following code in didFinishLaunchingWithOptions
if ([[UINavigationBar class] respondsToSelector:#selector(appearance)])
{
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed: 4.0/255.0 green:173.0/255.0 blue:214.0/255.0 alpha:1.0f ]]; //// change background color of navigationBar
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; /// set backButton color of navigation bar
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}]; // set title color
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal]; /// set all barButton item color
self.navController.navigationBar.translucent = NO;// set translucent NO
}
Use as per your requirement.
So there is some magic happening when you use a navigation controller that you don't see and now that you've made the switch to justing using a navigation bar you lose that magic.
Navigation bars are responsible for managing and presenting UNavigationItems, and as such hold an array of them. A NavigationItem holds things like left and right buttons, and the title or titleView. When your navigation controller pushes a new controller, it creates a brand new UINavigation item then links it to your new view controller, and then pushes that onto navigation bars stack of nav items. That's why from within your view controller you set the title with self.navigationItem.title instead of referencing the navbar.
Basically you have to manage the bar and nav items yourself now. This should about do it for you:
_navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, yOffset, CGRectGetHeight(size), height)];
_navItem = [[UINavigationItem alloc] initWithTitle:#"My navbar title"];
[_navBar setItems:#[_navItem]];
[self.view addSubview:_navBar];
Of course you'll have to manage the size a bit differently as my example comes from an app where it's being used in landscape mode.

Move a UIBarButton Item Down A Couple Pixels

I have a pop up window that slides on screen when a certain button is pressed.
This view has a custom look apart from the rest of the app. It has an .xib file as well. I have managed to change the header background color by adding a customClass to the toolbar in IB. I have managed to change the text style of the title text... It took me some time to figure that out. What I had to do was make an IBOutlet from the title item in IB, then apply the changes in my m...like so
- (void)customizeAppearance
{
//Sets custom text attribures for this views headers text
[_customNavText setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor, //sets text color
[UIColor blackColor], UITextAttributeTextShadowColor, //sets text shadow color
[NSValue valueWithUIOffset:UIOffsetMake(0, 3)], UITextAttributeTextShadowOffset, //Moves drop shadow by its coordinates
[UIFont fontWithName:#"Helvetica-Bold" size:27.0], UITextAttributeFont,nil] //sets text font and size
forState:UIControlStateNormal];
}
and this function gets called once the view loads. So my text has its own style on this view only. Great.
So, maybe I am getting to picky, but now I want to move the text down a couple pixels.
This is what I did, I added the following code inside the customizeApperance function...
[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(25.0f, 25.0f) forBarMetrics:UIBarMetricsDefault];
But this does something interesting, it does not move my text down for the UIBarButtonItem I have just applied those styles too, instead it moves the text of the buttons on the nav bar, such as the cancel button.
So how do I move the UIBarButtonItem thats in the middle, containing text for the navigation header, rather than the text of the buttons on the side?
I think you might be looking for:
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:1 forBarMetrics:UIBarMetricsDefault];

iOS - UIBarButtonItem - Back Button title vertical position not adjusting

I am setting a custom font in my UIBarButtonItem objects using UIAppearance. This works fine and sets the font correctly. However, I do need to adjust the button title's vertical position to cater for the sizing of the new font.
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObject:[UIFont fontWithName:#"Sketch Rockwell" size:12] forKey:UITextAttributeFont] forState:UIControlStateNormal];
[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, 1.5) forBarMetrics:UIBarMetricsDefault];
The problem is that the title's vertical position is adjusted correctly in a regular UIBarButtonItem, but the back buttons on the navigation bar do not get adjusted. I presume that I am targeting the correct object in my code because its font gets updated, just not its title's vertical position.
Does anyone have an idea of how to target the back buttons directly?
Thanks
Brian
I had this issue too, I fixed it by using:
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, 1.5) forBarMetrics:UIBarMetricsDefault];

Resources