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];
Related
I'm trying to set a UIBarButtonItem with an image. The problem is that it never appears. When I just add a UIBarButtonItem with a title it works.
UIBarButtonItem *openLeftSliderButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"filter.png"] style:UIBarButtonItemStylePlain target:self action:#selector(openLeftSlider)];
self.navigationItem.leftBarButtonItem = openLeftSliderButton;
UIImage *sideImage = [UIImage imageNamed:#"i1.jpg"];
UIButton *btnSideMenu = [UIButton buttonWithType:UIButtonTypeCustom];
[btnSideMenu setImage:sideImage forState:UIControlStateNormal];
[btnSideMenu setImage:sideImage forState:UIControlStateHighlighted];
[btnSideMenu setFrame:CGRectMake(0, 0, 20, 20)];
[btnSideMenu addTarget:self action:#selector(btnLeftMenuClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *sideMenuItem = [[UIBarButtonItem alloc] initWithCustomView:btnSideMenu];
self.navigationItem.leftBarButtonItem=sideMenuItem;
You can create UIBarButtonItem using initWithCustomView
_leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_leftButton setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[_leftButton addTarget:self action:#selector(leftButton:) forControlEvents:UIControlEventTouchUpInside];
[_leftButton sizeToFit];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_leftButton];
I want to add a custom image as left menu item. Thats why I'm using customView else I get my image but in blue.
This is how I achieve it:
UIImageView *menuItemImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MenuButton"]];
UIBarButtonItem *leftMenuItemButton = [[UIBarButtonItem alloc] initWithCustomView:menuItemImageView];
[leftMenuItemButton setAction:#selector(menuClicked:)];
[leftMenuItemButton setTarget:self];
self.navigationItem.leftBarButtonItem = leftMenuItemButton;
Everything works except when I click my button nothing happens. It never comes in this:
- (void)menuClicked:(id)menuClicked {
NSLog(#"%s menuClicked", __PRETTY_FUNCTION__);
}
Someone could help me?
The problem here seems to be using ImageView , UIBarButtonItem target and action only works if the custom view is UIButton.
Try something like this
UIButton* someBtn = [UIButton buttonWithType: UIButtonTypeInfoLight];
UIBarButtonItem *leftMenuItemButton = [[UIBarButtonItem alloc] initWithCustomView:someBtn];
[leftMenuItemButton setAction:#selector(menuClicked:)];
[leftMenuItemButton setTarget:self];
self.navigationItem.leftBarButtonItem = leftMenuItemButton;
Hope this helps
First, you need to create a custom UIButton and UIBarButtonItem
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(menuClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateHighlighted];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
Add the button to navigationBar
[self.navigationItem setLeftBarButtonItems:[NSArray arrayWithObjects:barButton, nil]];
Thanks to n00bprogrammer:
UIBarButtonItem * menuItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:#"MenuButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:#selector(menuClicked:)] ;
self.navigationItem.leftBarButtonItem = menuItem;
Both answers point to the right direction.
This is how I got it working:
UIButton *showCalendar = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
showCalendar.frame = CGRectMake( 0, 0, 40, 30 );
[showCalendar setImage:[UIImage imageNamed:#"calendar"] forState:UIControlStateNormal];
[showCalendar addTarget:self action:#selector(toggleCalendar) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:showCalendar];
Here is the code, add a tap gesture to your imageview:
UIImageView *menuItemImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MenuButton"]];
UIBarButtonItem *leftMenuItemButton = [[UIBarButtonItem alloc] initWithCustomView:menuItemImageView];
UITapGestureRecognizer *tapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(menuClicked:)];
[menuItemImageView addGestureRecognizer:tapGesture];
I want to incorporate a custom back button - I'm able to get the above result using
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back-btn"] style:UIBarButtonItemStylePlain target:nil action:nil];
but how do you remove the native blue button?
Use the below code to hide the back arrow:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back-btn"]
style:UIBarButtonItemStylePlain
target:nil
action:nil];
if ([UINavigationBar instancesRespondToSelector:#selector(setBackIndicatorImage:)]) {
[[UINavigationBar appearance] setBackIndicatorImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[[UIImage alloc] init]];
}
In iOS7+ you can use navigationBar's backIndicatorImage and backIndicatorTransitionMaskImage to achieve the custom arrow that you want. Swift code below:
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named:"button-backArrow18x15")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named:"button-backArrowMask18x15")
If you want to hide the "back" text you can set the title of the previous view to be an single space " " or use a custom UIBarButtonItem that doesn't have a title.
You can hide backbutton
self.navigationItem.hidesBackButton=YES;
To create custom leftbarbutton you can use this:
UIButton* someButton = [UIButton buttonWithType:UIButtonTypeCustom];
[someButton setFrame:frame];
[someButton setBackgroundImage:#"youArroImageName" forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem* someBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:someButton];
[self.navigationItem setLeftBarButtonItem:someBarButtonItem];
Check this one:
UIImage *faceImage = [UIImage imageNamed:#"back_arrow.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );
[face addTarget:self action:#selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:face];
self.navigationItem.leftBarButtonItem = backButton;
[self.navigationItem setHidesBackButton:YES animated:YES];
[self.navigationItem setBackBarButtonItem:nil];
-(IBAction)handleBack:(id)sender
{
// [self dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
Thanks to all the answerers - found a simpler way of doing it...
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back-btn"] style:UIBarButtonItemStylePlain target:self action:#selector(backButtonAction:)];
self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
[self.navigationItem setHidesBackButton:YES animated:NO];
[self.navigationItem setBackBarButtonItem:nil];
-(void) backButtonAction:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
Swift 4 (if you just want to hide it)
let mask = UIImage()
navigationController?.navigationBar.backIndicatorImage = mask
navigationController?.navigationBar.backIndicatorTransitionMaskImage = mask
Why can't you try with the Custom UIButton :
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:#"back-btn"] forState:UIControlStateNormal];
UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.backBarButtonItem = backBtn;
I just faced the exact same problem, here's what I ended up doing:
// First set the back button text to an empty string
viewController.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
// Set the back-indicator image to the custom image (scaled to 20x20)
UIImage *img = [MyUtils imageWithImage:[UIImage imageNamed:#"back"] scaledToSize:CGSizeMake(20, 20)];
[[UINavigationBar appearance] setBackIndicatorImage:img];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:img];
// Set the tint color to WHITE
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
Here's the image-scaling method:
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Credit for the scaling method above goes to Paul Lynch on this SO question: (The simplest way to resize an UIImage?)
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];
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];
}