Distorted navigation bar custom back button - iphone

I have a custom back button and the button is not displaying as intended, it is being stretched, and even though the back button text is empty it is still displaying the "back"
text.
Thanks in advance.

I usually use this technique to make it work:
To get rid default title:
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-400.f, 0) forBarMetrics:UIBarMetricsDefault];
Use resizable image:
UIImage *backButton = [UIImage imageNamed:#"back.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[backButton resizableImageWithCapInsets:UIEdgeInsetsMake(0, backButton.size.width, 0, 0)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Try something like
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = ... your image
[back setBackgroundImage:image forState:UIControlStateNormal];
[back addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
back.frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIBarButtonItem *backbi = [[UIBarButtonItem alloc] initWithCustomView:back];
self.navigationItem.leftBarButtonItem = backbi;
or post your code.

Related

customize UIBarButtonItem initWithBarButtonSystemItem to show up as grey not white in iOS 5

I am using a white background on my UINavigationBar with darkGrayColor text
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"uinavigationcontrollerbackground"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font,UITextAttributeFont ,[UIColor darkGrayColor], UITextAttributeTextColor, [UIColor whiteColor], UITextAttributeTextShadowColor, nil]];
Which looks nice but I have buttons defined like this throughout my app:
self.searchButton =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(searchProducts)];
I killed the default blakc background look I had going like this:
[[UIBarButtonItem appearance] setBackgroundImage:[UIImage imageNamed:#"uibarbuttonbackground"] forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
Which is just a white square.
Now my icons are still white with a light gray shadow, anyway to change these default icons?
Once I tried to change background of UIBarButtonSystemItemCamera but nothing worked. I guess these default system button types (UIBarButtonSystemItem )come with a background image already, so changing it won't do.
I recommend you to create a custom view and init your bar button with it. Not a perfect code but I hope it helps.
UIImage *image = [UIImage imageNamed:#"icon.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget:self action:#selector(takePicture:) forControlEvents:UIControlEventTouchUpInside];
UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
[v addSubview:button];
UIBarButtonItem *barItem = [[[UIBarButtonItem alloc] initWithCustomView:v] autorelease];
I used the category solution found here, it is working great:
UIBarButtonItem with custom image and no border
I little more work than I was hoping but it is a nice clean solution.

Making custom back button for navigation bar

I have made at least 10 buttons for my navigation bar, but it never seems to work right.
The rounded edges get pixelated. I cant have that in an app, so can anyone tell me how to make a good icon that looks like an apple one? Also what is the proper size? The code in the app for the button is
UIButton *backbtn = [UIButton buttonWithType:UIButtonTypeCustom];
backbtn.frame = CGRectMake(0, 0, 55, 30);
[backbtn setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[backbtn addTarget:self action:#selector(goBackOne) forControlEvents:UIControlEventTouchUpInside]; forState:UIControlStateNormal ];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbtn];//set new button
self.navigationItem.hidesBackButton = YES;//hide original back button
Try this code:
UIImage *backButtonImage = [UIImage imageNamed:#"backButton.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBackBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = customBackBarItem;
and, the back method:
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
The code above just set the image for normal state. You can also set highlighted state for a better appearance. Add some codes Like:
UIImage *backButtonImageHighlighted = [UIImage imageNamed:#"backButtonHighlighted.png"];
[backButton setImage:backButtonImageHighlighted forState:UIControlStateHighlighted];

How do I change the location of the back button on iOS?

You see I am making a completely custom navigation bar and when i create the back button it doesn't stay in the right place as you can see below the code.
here is my code for the app
- (void)viewDidLoad
{
[super viewDidLoad];
if(self.navigationController.viewControllers.count > 1) {
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setTitle:#"" forState:UIControlStateNormal];
[backButton setBackgroundImage:[UIImage imageNamed:#"backButton.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(didTapBackButton:) forControlEvents:UIControlEventTouchUpInside];
backButton.frame = CGRectMake(0.0f, 0.0f, 44.0f, 45.0f);
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
}
}
I think you can just modify contentEdgeInsets to make the position of the button.
Something like this:
[backButton setContentEdgeInsets:UIEdgeInsetsMake(5, 0, -5, 0)];
Come from UIButton Class Reference
contentEdgeInsets
The inset or outset margins for the rectangle surrounding all of the button’s content.
#property(nonatomic) UIEdgeInsets contentEdgeInsets
DiscussionUse this property to resize and reposition the effective drawing rectangle for the button content. The content comprises the button image and button title. You can specify a different value for each of the four insets (top, left, bottom, right). A positive value shrinks, or insets, that edge—moving it closer to the center of the button. A negative value expands, or outsets, that edge. Use the UIEdgeInsetsMake function to construct a value for this property. The default value is UIEdgeInsetsZero.
You can render a larger image with some spacing on the left.
UIImage *origImage = [UIImage imageNamed:#"button-thin-hamburger.png"];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(origImage.size.width + 10, origImage.size.height), NO, 0.0);
[origImage drawInRect:CGRectMake(10, 0, origImage.size.width, origImage.size.height)];
UIImage *buttonImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
[[UINavigationBar appearance] setBackIndicatorImage:buttonImage];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:buttonImage];
Well you can't change, you have to hide it.
Look at this page posted here:
http://rafaelsteil.com/2011/07/23/crappy-ios-apis-uinavigationcontroller/
Can't change back button title but I can hide it
you need to hide NavigationBar first...
[self.navigationController setNavigationBarHidden:YES animated:NO];
then create Custom UIButton..
UIButton *btn1=[UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(10, 0, 60, 40)];// Frame u want
[btn1 setImage:[UIImage imageNamed:#"BT_ipad_P_backbward#2x.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(Click_backward:) forControlEvents: UIControlEventTouchUpInside];
btn1.tintColor=[UIColor purpleColor];
[self.view addSubview:btn1];

Cant set image to backBarButtonItem

i have UITableView based application, and i can't set backBarButtonItem properly.
Back button is button, which appears when i select any item, call second view with method didSelectRowAtIndexPath. When i try to set it like this:
UIButton *back1=[UIButton buttonWithType:UIButtonTypeCustom];
[back1 setFrame:CGRectMake(10.0, 2.0, 45.0, 40.0)];
[back1 addTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
[back1 setImage:[UIImage imageNamed:#"back_button.png"] forState:UIControlStateNormal];
UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:back1];
self.navigationItem.backBarButtonItem = back;
There is just back button with default style and somehow with title of my TableView controller window.
When i set it like this:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"back_button.png"]
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
There is an image, but, it look like "inserted" in default back button, doesn't look nice at all.
Please help me.. i tried many ways to solve this, but, in many cases there is just default Back button with title "Products" (there is title which appears on top of navigation bar in my UITableView). Any help would be appreciated!
UIImage *buttonImage = [UIImage imageNamed:#"back_button.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
backButton.font = [UIFont boldSystemFontOfSize:12];
backButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[backButton addTarget:self action:#selector(showMeter)
forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:backButton];
//Create resizable images
UIImage *myButton = [[UIImage imageNamed:#"yourButton"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
//Set the background image for *all* UIBackBarButtons
[[UIBarButtonItem appearance] setBackgroundImage:myButton forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
You should assign it to self.navigationItem.leftBarButtonItem.
self.navigationItem.leftBarButtonItem = backButton;

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!