Move a UIBarButton Item Down A Couple Pixels - ios

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];

Related

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 set the UISearchBar cancel button highlight color

I have a UISearchBar that is added to my view controller in a xib file.
I have the following properties set through Interface Builder:
barStyle = UIBarStyleDefault
searchBarStyle = UISearchBarStyleMinimal
translucent = YES
barTintColor = Clear Color
In code I'm styling the bar with this code, to make the text color white:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];
Also in my app delegate didFinishLoading method I have:
self.window.tintColor = [UIColor whiteColor];
My cancel button in the UISearchBar is a light grey. When I select it it highlights blue, which is jarring as it doesn't fit with any of the other visual elements in the screen. How can I change the button to highlight to a different color? Alternatively I would be fine if the cancel button was white and highlighted to a grey, like my navigation bar buttons do, but I don't know how to change the button color.
I'm targeting iOS7, using Xcode 5.
This answer seems appropriate, but it is having no effect for me https://stackoverflow.com/a/15791136/472344
Try this in Appdelegate in didFinishLaunchingWithOptions
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],UITextAttributeTextShadowOffset,nil] forState:UIControlStateNormal];
Try to set tintColor of the Search Bar. it work for me with target 8.0 xcode 5.
self.searchBar.tintColor = [UIColor blackColor];

UIBarButtonItem Title Text is Always Global Tint Color

iOS 7.0 and up.
In AppDelegate.m, I'm setting my global tintColor: self.window.tintColor = myGlobalTintColor;.
In my table view controller where I want a red trash can button in the navigation bar while editing the table view cells, I have this:
- (void)setEditing:(BOOL)editing animated:(BOOL)animate {
[super setEditing:editing animated:animate];
if (editing) { // Start editing
self.deleteBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:#selector(deleteButtonPressed)];
[self.deleteBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItems = #[self.deleteBarButtonItem];
}
}
Despite the line of code that is setting NSForegroundColorAttributeName to [UIColor redColor], I never see the red color. When the button it set to .enabled = YES, I simply see it in my global tint color, like all the other buttons:
I've combed through my code and I'm sure there are no other locations where I'm setting anything else to myGlobalTintColor. It's worth noting that this UITableViewController is instantiated from a storyboard. Commenting out the line in AppDelegate.m just sends everything back to the default blue tintColor, but I still don't get the red color on the delete button.
It turns out the problem was that I was using a third party library, SDCAlertView, and I was setting
[[SDCAlertView appearance] setTintColor:globalTintColor];
For some reason, this caused all of my UINavigationBar objects to take on it's tintColor.
I fixed this by simply replacing the above code:
[[SDCAlertView appearance] setButtonTextColor:globalTintColor];

Center navigation bar title vertically

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)

Changing text color UINavigationBar

Hi i'm working on an iOS project that uses the MFMailComposeViewController to send out an email.
I changed the title bar to use an image
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"project_headerbg"] forBarMetrics:UIBarMetricsDefault];
but now when I click on the '+' button next to CC I can't read the text "Choose a contact to mail" in the header very well and was wondering if there was a way to change the color of that text?
Thanks
This question has nothing to do with using the MFMailComposeViewController. If you wish to customize the appearance of a UINavigationBar, then you may need to set more properties than just the background image. You should also setup the titleTextAttributes.
// Use whatever color is appropriate
NSDictionary *attributes = #{ UITextAttributeTextColor: [UIColor blueColor] };
[[UINavigationBar appearance] setTitleTextAttributes:attributes];
There are other attributes such as the font and shadow colors. Feel free to set the ones you need.

Resources