IOS - Vertically Align the Back Button in NavigationBar - ios5

I newbie in iOS Programming.
i Have a cutom navigation bar that have 70 px height and i want that back button and Title is vertically align to middle.
I alredy done with the navbar title.
But i dont have any idea how to vertically align the back button to middle.
thanks.

In iOS 5 you can customise the position of the back buttons of all of your navigation bars with this magic incantation:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setBackButtonBackgroundVerticalPositionAdjustment:-10 forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setBackButtonBackgroundVerticalPositionAdjustment:-10 forBarMetrics:UIBarMetricsLandscapePhone];
This will move the back button 10 pixels upwards.
A good place to put this code is in your application delegate's application:didFinishLaunchingWithOptions: method.
Or if you just want to customise one button:
[myBackButton setBackButtonBackgroundVerticalPositionAdjustment:-10 forBarMetrics:UIBarMetricsDefault];
[myBackButton setBackButtonBackgroundVerticalPositionAdjustment:-10 forBarMetrics:UIBarMetricsLandscapePhone];
More information:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIBarButtonItem_Class/Reference/Reference.html

Set the back bar button's center.y to the imageview's height/2.

Related

UINavigationBar handle rotation

I'm trying to handle the rotation of a UINavigationBar with UINavigationItem (i'm not using the UINavigationController), i have successfully made so that the height and width show according, but that does not seems to be correct, as the UINavigationBar still acts as being in portrait mode (big title and button), also when i set a custom background for both metrics, it seems to only show the portrait background metric, ignoring completely the landscape one.
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"modal-top-landscape"] forBarMetrics:UIBarMetricsLandscapePhone];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"modal-top"] forBarMetrics:UIBarMetricsDefault];
Is there a way to tell the UINavigationBar that is rotated and should show the text and buttons accordingly?
I attach some screenshots:
One workaround for this is to use a UINavigationController, and use a CGRectOffset to remove the margin that leaves the status bar... add the Controller to the parent controller (also the view).

ABPeoplePickerNavigationController "Groups" View Navigation Controller

I'm using a custom Navigation Bar appearance in my app with this code in the App Delegate's application:didFinishLaunchingWithOptions: method:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navBar.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundColor:[UIColor clearColor]];
However, this appearance breaks when I present an ABPeoplePickerNavigationController (to allow selection of a contact to populate the To: field for a new email), because the system uses an extra tall UINavigationBar when this view is showing due to the prompt property on UINavigationItem being set by the system ("Choose a contact to mail").
The fix is to add this code:
[[UINavigationBar appearanceWhenContainedIn:[ABPeoplePickerNavigationController class], nil] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearanceWhenContainedIn:[ABPeoplePickerNavigationController class], nil] setBarStyle:UIBarStyleBlack];
Which looks like this:
However, this little hack doesn't work when you tap the Groups button from the initial view. It still looks broken due to the extra tall Navigation Bar:
Any ideas what the controller name is for that view (so I can apply the same exclusion as above), or another way to fix this?
I'm just hitting this now.. any luck resolving? I'm thinking to try interating through the ABPeoplepickerNavigationControllers view controllers and applying appearanceWhenContainedIn method...
I've been fighting with this as well, but think I've pieced together a solution.
The group selection view is some other (unknown to me, likely private) class, so we can't specify an exception style through an appearance-proxy-when-contained-in approach, as you've done for the ABPeoplePickerNavigationController. Instead, we should try and resolve the original issue, which is the custom background image not rendering properly when the prompt is shown and the navigation bar is tall.
The solution here (iOS5 UINavigationBar background image issues when prompt is shown) suggests using a resizable background image for the navigation bar.
That almost worked for me, but the background image I was using included the shadow for underneath the navigation bar and that wasn't resizing correctly when the prompt was shown. Instead, I had to use a resizable background image without a shadow and then specify the shadow image separately.
UINavigationBar* navigationBar = [UINavigationBar appearance];
[navigationBar setBackgroundImage:[[UIImage imageNamed:#"TopBarBackgroundNoShadow"] resizableImageWithCapInsets:UIEdgeInsetsMake(3, 0, 3, 0)]
forBarMetrics:UIBarMetricsDefault];
[navigationBar setShadowImage:[UIImage imageNamed:#"TopBarShadowResize"]];
Using this approach removed the need for any exception styling and looked good in both the ABPeoplePickerNavigationController and the group selection view, as well as at standard height.

UINavigationController toolbar buttons not animating when pressed

I have few UIViewControllers embeded in NavigationController, everything is cool when it comes to change screen titles, button titles, hiding/showing navigation bar (top bar) or toolbar (bottom bar), but - I cannot force toolbar buttons to have this animated shadow when pressed, as navigation bar buttons have. What's more, toolbar buttons are as black as toolbar - shouldn't button be slightly lighter color than toolbar?
I hope it's clear, because I couldn't find it nor even come up with reasonable title
If you want a black bar (navigation or toolbar), set its barStyle to UIBarStyleBlack in addition to or instead of tinting it black. This lets it know that the buttons should be tinted differently than if the bar was any other color.
I am not able to understand you whole problem but for the toolbar problem you can give those buttons different colors and also can give an effect of changing color of button which is clicked
UIBarButtonItem *toolBtn = [[UIBarButtonItem alloc] initWithTitle:#"Share and Upload"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(your_Target)];
toolBtn.tintColor = [UIColor redColor];
Now suppose toolBtn is your first button in toolbar and on click of it you want to change its color then in your target function
-(void)your_Target{
UIBarButtonItem *button1 = [[your_Toolbar items] objectAtIndex:0];
button1.tintColor = [UIColor brownColor];
// or you can simply loop all toolbar buttons and change the color of only clicked one and for rest keep it default
}
Hope this will help you.. and sorry for my bad english :)

How do you target particular UIViews when styling its UIAppearance?

I am setting global styles in iOS 5 with UIAppearance. Here's an example:
[[UIbarButtonItem appearance]
setTtitletextAttributes:someStyle
forState:UIControlStateNormal
];
It looks good in most cases:
But there are cases where the global style looks ugly, like in a movie player.
For the movie player, I would prefer to have the default blue button. So how would I target only the back button's appearance and not the done button's appearance? I have similar issues with targeting normal table cells and grouped table cells.
You need to use a custom subclass of UINavigationBar. Let's call it MyNavigationBar. Then you can do this:
[[UIBarButtonItem appearanceWhenContainedIn:[MyNavigationBar class], nil]
setTintColor:[UIColor redColor]];
and it will only affect buttons within your navigation bar, not the MPMoviePlayerController's navigation bar.
The problem, of course, is that UINavigationController always uses a basic UINavigationBar... if you create it in code. But if you create it in a nib, you can click on its navigation bar (in the nib's document outline) and change the bar's class in the Identity inspector.
If in your AppDelegate you just replace
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navback"]
forBarMetrics:UIBarMetricsDefault];
with
[[UINavigationBar appearanceWhenContainedIn:[UINavigationController class], nil]
setBackgroundImage:[UIImage imageNamed:#"navback"] forBarMetrics:UIBarMetricsDefault];
movie player will have proper black bar in full screen mode. Works in iOS 5 and iOS 6
You can actually change the proxy to just target certain view hierarchies. In other words, when the view you want to customize is contained in a certain view, you can modify its appearance differently from the rest. So set your global (white) style first, but then call the following method to customize the appearance for when the button appears in an MPMoviePlayerController or whatever else.
[[UIBarButtonItem appearanceWhenContainedIn:
[MPMoviePlayerController class], nil]
setTitletextAttributes:someStyle
forState:UIControlStateNormal];
Let me know if that helps!

How to make image insets for image within UISegmentedControl?

I have UISegmented control with image -to be able to set background tint. This is added to navigation bar through code in viewDidLoad:
UISegmentedControl *myCustomButton = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:[UIImage imageNamed:#"spiral"],nil]];
[myCustomButton setSegmentedControlStyle:UISegmentedControlStyleBar];
[myCustomButton setTintColor:[UIColor blackColor]];
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc]
initWithCustomView:myCustomButton];
self.navigationItem.rightBarButtonItem = segmentBarItem;
problem is that the image within the button gets stretched all the way it can, so I would like to use image insets, but how do I call them for that code?
I've tried this:
[self.navigationItem.rightBarButtonItem setImageInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
but it does not do anything visible.
I'm not sure there's an easy programmatic way to do this. What I've done is just make png images with transparent backgrounds in photoshop (or whatever image editor) and make them fit so that your image fits vertically in the button and has a size that will fit just about right in your button. It's a bit of work to make the images, but usually you're not going to be changing your button size once you have the screen layout set up.