Could not trigger bar button item with custom view? - iphone

In my iphone application,
I have a one navigation bar Image to set I have set it like this....
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navigationBarReady.png"] forBarMetrics:UIBarMetricsDefault];
}
UIBarButtonItem *bbiLeft=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"btnBack.png"] style:UIBarButtonItemStylePlain target:self action:#selector(btnBackPressed:)];
[bbiLeft setTintColor:[UIColor clearColor]];
[bbiLeft setBackgroundVerticalPositionAdjustment:7.0f forBarMetrics:UIBarMetricsDefault];
self.navigationItem.leftBarButtonItem=bbiLeft;
It appears like this...
I want to set its color as navigationbar's background
How??
Thanks..

Get the Answer...
/* Back Button setted*/
UIButton *btnBack=[UIButton buttonWithType:UIButtonTypeCustom];
[btnBack addTarget:self action:#selector(btnBackPressed:) forControlEvents:UIControlEventTouchUpInside];
[btnBack setImage:[UIImage imageNamed:#"btnBack.png"] forState:UIControlStateNormal];
[btnBack setFrame:CGRectMake(0, 0, 51,16)];
UIView *backModifiedView=[[UIView alloc] initWithFrame:btnBack.frame];
[btnBack setFrame:CGRectMake(btnBack.frame.origin.x, btnBack.frame.origin.y+7, btnBack.frame.size.width, btnBack.frame.size.height)];
[backModifiedView addSubview:btnBack];
UIBarButtonItem *bbiLeft=[[UIBarButtonItem alloc] initWithCustomView:backModifiedView];
self.navigationItem.leftBarButtonItem=bbiLeft;
/* Arpit */
//set Navgation Bar.
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]){
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navigationBarReady.png"] forBarMetrics:UIBarMetricsDefault];
}

Setting the tintColor to clear doesn't work, you should pick a color that matches the bar background.
Also, you may need to set the tintColor of the UINavigationBar, not the tintColor of the UIBarButtonItem itself, as the navigation bar is responsible for tinting it's own button items.

Related

change background of back button to the same as navigation bar

I am trying to implement a custom image for the navigation bar and buttons on it. I managed to get the correct setup for my nav background using this
- (void)customAppearance
{
UIImage* NavBG = [UIImage imageNamed:#"header.png"];
[[UINavigationBar appearance] setBackgroundImage:NavBG forBarMetrics:UIBarMetricsDefault];
}
but when i try to do something similar for the back button or other button it all just blends together (I can see the words of the back button but no outline for the back button is present)
here is what i have for the back button
UIImage *backButtonBG = [[UIImage imageNamed:#"header.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 6)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonBG forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
and i want to get it looking like this:
I think that the easiest way is for you to also tint the navigation bar, because the default is that those bar butons items always use the color of the navigation bar.
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
or u can use a more precise color using rgb values
[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:val green:val blue:val alpha:1]];
I created my tinted button using a custom image (see code at the end) Then just set the leftBarButtonItem to this button.
+ (UIBarButtonItem *)createBarButtonItemWithImage:(UIImage *)normalImage highlightedImage:(UIImage *)highlightedImage
{
UIBarButtonItem *barButtonItemToReturn;
if (normalImage == nil && highlightedImage == nil)
{
return barButtonItemToReturn;
}
else if (normalImage != nil)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:normalImage forState:UIControlStateNormal];
if (highlightedImage != nil)
{
[button setImage:highlightedImage forState:UIControlStateHighlighted];
[button setFrame:CGRectMake(0, 0, normalImage.size.width, normalImage.size.height)];
}
barButtonItemToReturn = [[UIBarButtonItem alloc] initWithCustomView:button];
}
return barButtonItemToReturn;
}
When you want to add a target/action, you have to add it via:
[((UIButton*)barButtonItem.customView) addTarget:self selector:#selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];
You can also use a resizable image, etc

iOS: changing the tint color of the back button?

I have a navigation controller that pushes a UIViewController. I would like to change the tint color of the back button of the navigation item when a user presses on a certain button. Is this possible? I have tried using [UIBarButtonItem appearance] setTintColor: but it only works on initialization (for example in viewDidLoad) and not otherwise.
Try this......
You need to use a UIBarButtonItem with a custom view. Something like this:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
[button addTarget:target action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"back_button.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"back_button_tap.png"] forState:UIControlStateHighlighted];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
And put the button to the navigation bar, usually in a controller with UINavigationController:
self.navigationItem.leftBarButtonItem = buttonItem;
The easiest thing for me was to set the tint color for ALL uibarbutton items:
[[UIBarButtonItem appearance]setTintColor:[UIColor yourColor]];
And then explicitly setting the tintcolor for explicit navigationbuttons that I create & place on the navigationbar to other colors...
Saves me the headache of creating custom backbuttons when all I want to do is change the tint.
In the method that is called on your button click, just put [self.navigationItem.backBarButtonItem setTintColor:[UIColor *whateverColorYouWant*]]; I haven't tested it but I'm 99% sure that would work
Edit:
Just tested it, it works.
Here's a code that changes text and color of the back button:
- (void)viewDidLoad
{
UIBarButtonItem *backButton = [UIBarButtonItem new];
[backButton setTitle:#"Back"];
[backButton setTintColor:[UIColor yellowColor]];
[[self navigationItem] setBackBarButtonItem:backButton];
}
Try this:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class],[UIToolbar class], nil] setTintColor:[UIColor redColor]];

Problems with BackButtonItem

Well, I have a NavigationController with a diferent color and I want the back button to also have a background image, a highlighted background image, and a custom text color. How can I do this? I have tried lots of things but I havent been able to change this backbutton.
I guess you have to create your own custom button.
Create a custom UIBarButtonItem that suits your requirement.
Assign this Custom UIBarButtonItem to the leftBarButtonItem of the NavigationItem.
You can only point to the leftBarButtonItem, since the backBarButtonItem is ReadOnly.
self.navigationItem.leftBarButtonItem = aCustomBarButtonItem;
self.navigationItem.hidesBackButton = YES;
Hope this helps...
EDIT
Code that can help :
UIButton *aCustomButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aCustomButton setBackgroundImage:[UIImage imageNamed:#"back1.png"] forState:UIControlStateNormal];
[aCustomButton setBackgroundImage:[UIImage imageNamed:#"back2.png"] forState:UIControlStateHighlighted];
[aCustomButton addTarget:self action:#selector(onClickOfBack) forControlEvents:UIControlEventTouchUpInside];
[aCustomButton setFrame:CGRectMake(0, 0, 50, 40)];
UIBarButtonItem *aCustomBarButton = [[UIBarButtonItem alloc] initWithCustomView:aCustomButton];
self.navigationItem.leftBarButtonItem = aCustomBarButton;
[aCustomBarButton release];

iOS: Positioning navigation bar buttons within custom navigation bar

I'm building an app with a custom navigation bar. After some research I decided to do this using a category on UINavigationBar. The navigation bar needs to be a bit larger than usual to accomodate a drop shadow. Here is the code:
#import "UINavigationBar+CustomWithShadow.h"
#implementation UINavigationBar (CustomWithShadow)
- (void)drawRect:(CGRect)rect {
// Change the tint color in order to change color of buttons
UIColor *color = [UIColor colorWithHue:0.0 saturation:0.0 brightness:0.0 alpha:0.0];
self.tintColor = color;
// Add a custom background image to the navigation bar
UIImage *image = [UIImage imageNamed:#"NavBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, 60)];
}
- (void)layoutSubviews {
self.frame = CGRectMake(0, 20, self.frame.size.width, 60);
}
#end
The only problem now is that the larger navigation bar means that the navigation bar buttons end up too far down, like so:
Does anyone know how I can correct the position of the buttons?
Thanks for all help!
Update:
I add the buttons to the nav bar in the init method of the view controller like so:
// Create "Add" button for the nav bar
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(createNewEntry:)];
[[self navigationItem] setRightBarButtonItem:addButton];
[addButton release];
You'll need to add the leftBarButtonItem and rightBarButtonItem as custom items and mess with the frames.... for example:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0,5,buttonImage.size.width,buttonImage.size.height)];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button addTarget:delegate action:#selector(barButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:titleString forState:UIControlStateNormal];
[button setTitleColor:CUSTOM_BAR_BUTTON_TITLE_COLOR forState:UIControlStateNormal];
[[button titleLabel] setFont:[UIFont boldSystemFontOfSize:14]];
[[button titleLabel] setShadowColor:CUSTOM_BAR_BUTTON_SHADOW_COLOR];
[[button titleLabel] setShadowOffset:CGSizeMake(0,-1)];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[button release];
[[self navigationItem] setRightBarButtonItem:barButton];
[barButton release];
Try adding the buttons to the nav bar in the viewDidLoad method of the view controller instead.
My solution, not the best, but it works for me fine. My custom navigation bar has height 55 (default height is 44). I cut from my custom navigation bar only 44 of height and insert it to the navigation bar. Then I cut next part of my custom navigation bar (shadows etc.) and insert it as image view under the navigation bar. And that's it. Buttons are at right places...

iPhone custom UINavigationBar buttons

I have an application with 4 tabs. Each tab is a UINavigationController. The 4 UINavigationBar tabs should look the same, have a custom background image, a custom backButton and a custom right button triggering a function.
I would like to do those customizations only once in my code and not in each RootViewController.
I managed to have a custom background image by inserting this code into my appDelegate:
#implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: #"MyNavigationBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
But I didn't manage to customize the back and right buttons or specify the action for the right button.
Is there a way to do that in the appDelegate, just like for the background image?
Or should I do the customization in each RootViewController?
Write the below code in viewWillAppear method
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *butImage = [[UIImage imageNamed:#"back.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[button setBackgroundImage:butImage forState:UIControlStateNormal];
[button addTarget:self action:#selector(gotoBack:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, 48, 30);
UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
self.navigationItem.leftBarButtonItem = backButton;
and write the action event for backButton.
-(IBAction)gotoBack:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
In your appdelegate in aplicationDidFinishLaunching.....
UIImage *navBG = [UIImage imageNamed:#"barra-logo-centro.png"];
[[UINavigationBar appearance] setBackgroundImage:navBG forBarMetrics:UIBarMetricsDefault];
//backbutton: 22x30 , 44x60#2x
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:#"back_button.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:[UIImage imageNamed:#"normal_button.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
this will modify the complete proyect navigationbar and bar button ítems
Renungas answer works fine.
If you don't want to write the same code 4 times you can
always subclass UINavigationController.
I just tried this solution (with subclassing UITabBarController but nevertheless...)
and it works ok.
You can find similiar example here
Your custom code (identical to the mentioned example) should look like this:
- (void)loadView {
[super loadView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"backbutton.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}
- (void)backAction {
[self.navigationController popViewControllerAnimated:YES];
}
As you can see all you have to do is override loadView and add a method
to perform the popVievController selector.
Good luck!