Hide Back text in navigation item - ios

I need to set a back arrow by replacing back Text(button)in navigation bar. I can view the back arrow but i don't know how to hide the back Text in navigation item. Here's is my code:
with this am getting warning : : CGImageCreateWithImageProvider: invalid image size: 0 x 0.
(void)viewWillAppear:(BOOL)animated
{
UIImage *backButtonHomeImage = [[UIImage imageNamed:#"back-arrow.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 104, 13)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonHomeImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.navigationItem.leftBarButtonItem = nil;
}

The answer
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if([self.navigationController.viewControllers objectAtIndex:0] != self)
{
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
[backButton setImage:[UIImage imageNamed:#"back-arrow.png"] forState:UIControlStateNormal];
[backButton setShowsTouchWhenHighlighted:TRUE];
[backButton addTarget:self action:#selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *barBackItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.hidesBackButton = TRUE;
self.navigationItem.leftBarButtonItem = barBackItem;
}
}
-(void)popViewControllerWithAnimation
{
[self.navigationController popViewControllerAnimated:YES];
}
may it will help.

Try this :
UIImage *backButtonHomeImage = [[UIImage imageNamed:#"back-arrow.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 104, 13)];

Create custom back button and add that button to Navigation controller's back button.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,54,32);
UIImage *backButtonImage = [UIImage imageNamed:#"back-arrow.png"];
[button setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[button addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];

Just set blank text to title of navigation bar before pushing to that view Controller as below code.
self.navigationItem.title=#" ";

I hope this would work
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];

Related

Custom Back Button for Navigation Bar

I made a category of UINavigationBar as follow:
UINavigationBar (UINavBar_Category)
I want to create custom back button using this.
I can able to set image by following code:
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:#"btn_back.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Problem can not able to change title of back button.
My try
- (void)didMoveToSuperview
{
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:lang(#"btnBack") style:UIBarButtonItemStylePlain target:nil action:nil];
self.topItem.backBarButtonItem=backButton;
}
Help me to solve this!
Thanks in advance
Try this code instead.. Here a button is first created , its title and image is set and then it is set as left bar button item of navigation controller.
UIButton *backBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 29)];
[backBarButton setTitle:#" Log Out" forState:UIControlStateNormal];
[backBarButton setImage:[UIImage
imageNamed:#"leftarrow_ipad.png"] forState:UIControlStateNormal];
[backBarButton addTarget:self action:#selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBarButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
Try this
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if([self.navigationController.viewControllers objectAtIndex:0] != self)
{
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 26)];
[backButton setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal]; // some image
[backButton setShowsTouchWhenHighlighted:TRUE];
[backButton addTarget:self action:#selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *barBackItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.hidesBackButton = TRUE;
self.navigationItem.leftBarButtonItem = barBackItem;
}
}
-(void)popViewControllerWithAnimation
{
[self.navigationController popViewControllerAnimated:YES];
}

Positioning image of navigationBar backButton

I add an image to my navigation controller back button like this:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"ITButton.png"] style:UIBarButtonItemStyleBordered target:nil action:nil];
It works fine, therefore, my image is not aligned with the < back symbol. The image is a little above the symbol, I want both centered. I don't know why. I've tried to adjust this using:
[self.navigationItem.backBarButtonItem setImageInsets:UIEdgeInsetsMake(10, 0, 0, 0)];
but nothing changed.
there are many ways to fix the issue, I was facing the same issue then I have Solved it this way
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if([self.navigationController.viewControllers objectAtIndex:0] != self)
{
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
[backButton setImage:[UIImage imageNamed:#"home.png"] forState:UIControlStateNormal];
[backButton setShowsTouchWhenHighlighted:TRUE];
[backButton addTarget:self action:#selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *barBackItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.hidesBackButton = TRUE;
self.navigationItem.leftBarButtonItem = barBackItem;
}
}
-(void)popViewControllerWithAnimation
{
[self.navigationController popViewControllerAnimated:YES];
}

iOS custom right navigation bar

I'm trying to set an image for a right bar item in my navigation controller but iOS 6 keeps showing a black glow. I have tried a number of solutions from stack overflow but can't get it to work. The current code I have is this:
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"gear"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(someMethod)];
[rightItem setImageInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
[[self navigationItem] setRightBarButtonItem:rightItem];
In iOS7 is looks like this which is what I want:
This is how it looks in iOS6
try this one :
UIImage *faceImage = [UIImage imageNamed:#"gear.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );//set bound as per you want
[face addTarget:self action:#selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:face];
self.navigationItem.rightBarButtonItem = backButton;
may it will help you.
Create a UIButton first with the image, then do:
[[UIBarbuttonItem alloc] initWithCustomView:button];
Use custom view:
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; // change this to use your image
[rightButton addTarget:self
action:#selector(yourAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = rightButtonItem;
Try this:
UIImage* image = [UIImage imageNamed:#"sample_Image.png"];
CGRect frameimg = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(MethodName:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBtn =[[UIBarButtonItem alloc] initWithCustomView:someButton];
[self.navigationItem setRightBarButtonItem:barBtn];

UIBarButtonItem background image and setting action

I created button in navigation bar with this code:
UIBarButtonItem *myButtonEn = [[UIBarButtonItem alloc] initWithTitle:#"EN" style:UIBarButtonItemStyleDone target:self action:#selector(buttonEN:)];
My action for this button:
- (void) buttonEN:(id)sender {
NSLog(#"English language");
}
This works fine but I have to change background and button's style (code below).
UIImage *imageEN = [UIImage imageNamed:#"cys_lang_en.png"];
UIButton *buttonImgEN = [UIButton buttonWithType:UIButtonTypeCustom];
buttonImgEN.bounds = CGRectMake( 0, 0, imageEN.size.width, imageEN.size.height );
[buttonImgEN setImage:imageEN forState:UIControlStateNormal];
UIBarButtonItem *myButtonEn = [[UIBarButtonItem alloc] initWithCustomView:buttonImgEN];
This is axactly what I need, but I need to set action
[myButtonEn setTarget:self];
[myButtonEn setAction:#selector(buttonEN:)];
This code is compiled with no errors but button does not respond.
Does anyone know what is wrong?
try this..
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 40.0f)];
[btn addTarget:self action:#selector(buttonEN:) forControlEvents:UIControlEventTouchUpInside];
[btn setImage:[UIImage imageNamed:#"cys_lang_en.png"] forState:UIControlStateNormal];
UIBarButtonItem *eng_btn = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = eng_btn;
}
-(void)buttonEN:(id)sender
{
NSLog(#"English language");
}
You are Missing action and target for button
You can set using,
[myButtonEn setTarget:self];
[myButtonEn setAction:#selector(buttonEN:)];
You might want to change apperance of this button.
[[UIBarButtonItem appearance] setBackgroundImage:<doneBackgroundImage>
forState:UIControlStateNormal
style:UIBarButtonItemStyleDone
barMetrics:UIBarMetricsDefault];
You don't need to use custom view for image,
Try to use:
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:yourImage style:UIBarButtonItemStylePlain target:target action:#selector(selector)];

Can't customize pushViewController's back button

I've got a ViewController which opens up another ViewController by
[self.navigationController pushViewController:detailViewController animated:YES];
Now the new VC has got a back button in the navBar but I can't manage to give it the same background image as the navBar buttons from the first ViewController. I've tried with:
UIImage *buttonImage = [UIImage imageNamed: #"Button.png"];
[self.navigationItem.backBarButtonItem setBackButtonBackgroundImage:buttonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem.backBarButtonItem setTitle:#"doesnt work either"];
But it doesn't work. What am I doing wrong? Thank you!
Try code snippet give below, it works fine for me:
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(pop)];
self.navigationItem.backBarButtonItem = barBtnItem;
Or you can do this as well:
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
[backButton setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[backButton setShowsTouchWhenHighlighted:TRUE];
[backButton addTarget:self action:#selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *barBackItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.hidesBackButton = TRUE;
self.navigationItem.leftBarButtonItem = barBackItem;
Enjoy Coding :)
Try this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Button.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(pushNav:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
-(void)pushNav {
[self.navigationController popViewControllerAnimated:YES];
}

Resources