I created a custom button and applied it to the UIbarbuttonitem.
There is no error but nothing is shown on the navigation bar:(
This is my code-
//create a custom button
UIImage *image = [UIImage imageNamed:#"TESTButton.png"];
UIButton *myCustomButton = [UIButton buttonWithType:UIButtonTypeCustom];
myCustomButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[myCustomButton setImage:image forState:UIControlStateNormal];
[myCustomButton addTarget:nil action:#selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myCustomButton];
self.navigationItem.leftBarButtonItem = button;
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[button release];
[myCustomButton release];
[image release];
[navID release];
Anybody who can fix my code? :)
From the docs:
initWithImage:style:target:action:
Try that.
To set ANY image with right coloring, use UIButton, set "image for state" and then create UIBarButtonItem with customView
UIImage *btnImage = [UIImage imageNamed:#"button"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.bounds = CGRectMake( 0, 0, btnImage.size.width, btnImage.size.height );
[btn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchDown];
[btn setImage:btnImage forState:UIControlStateNormal];
_buttonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
One of the Simplest Way is :
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"backk.png"] style:UIBarButtonItemStylePlain target:self action:#selector(backBtn:)];
self.navigationItem.leftBarButtonItem = button2;
Action Method:
- (IBAction)backBtn:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
Related
I am using below code for Navigation right bar button:--
UIButton *settingsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[settingsBtn setImage:[UIImage imageNamed:#"logo_soft.png"] forState:UIControlStateNormal];
//[settingsBtn addTarget:self action:#selector(settings) forControlEvents:UIControlEventTouchUpInside];
[settingsBtn setFrame:CGRectMake(-16, -6, 95, 43)];
UIView *rightBarButtonItems = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 76, 32)];
//[rightBarButtonItems addSubview:homeBtn];
[rightBarButtonItems addSubview:settingsBtn];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButtonItems];
Now I want to use below code on same Navigation right bar button:
- (void)viewDidLoad
{
[super viewDidLoad];
// Animation
UIImageView*animationView = [[UIImageView alloc] initWithFrame:self.view.frame];
animationView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.gif"],
[UIImage imageNamed:#"2.gif"],
[UIImage imageNamed:#"3.gif"],
[UIImage imageNamed:#"4.gif"],
[UIImage imageNamed:#"5.gif"],
[UIImage imageNamed:#"6.gif"],nil];
animationView.animationDuration = 1.25;
animationView.animationRepeatCount = 0;
[animationView startAnimating];
[self.view addSubview:animationView];
}
How to work my navigation bar button like the below Images:---
Please send me suitable answer or provide me sample code.
Thank you.
I have tried your code, with a little trick of mine and it work like magic.
NSArray *imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"tmp-0"],
[UIImage imageNamed:#"tmp-1"],
[UIImage imageNamed:#"tmp-2"],
[UIImage imageNamed:#"tmp-3"],
[UIImage imageNamed:#"tmp-4"],
[UIImage imageNamed:#"tmp-5"],
[UIImage imageNamed:#"tmp-6"],
[UIImage imageNamed:#"tmp-7"],nil];
UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barButton setImage:[UIImage imageNamed:#"tmp-0"] forState:UIControlStateNormal]; // mine trick
[barButton.imageView setAnimationImages:imageArray];
[barButton.imageView setAnimationDuration:1.0f];
[barButton.imageView startAnimating];
[barButton sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:barButton];
You need to give a image to barButton at first, so your button can make its frame
self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
I have done above,but it doesn't work.The color is still as same as the background color!
Create button and substitute
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.leftBarButton];
self.navigationItem.leftBarButtonItem = leftBarButton;
or
+ (UIBarButtonItem*)itemWithNormalImage:(UIImage*)normalImage
pressedImage:(UIImage*)pressedImage
target:(id)target
action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:normalImage
forState:UIControlStateNormal];
[button setImage:pressedImage
forState:UIControlStateSelected];
[button setImage:pressedImage
forState:UIControlStateHighlighted];
button.frame = CGRectMake(0, 0, normalImage.size.width, normalImage.size.height);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* item=[[UIBarButtonItem alloc] initWithCustomView:button];
return item;
}
Text color:
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:#"done" style:UIBarButtonItemStyleBordered target:self action:nil];
[item setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItems = #[item];
is there any way to change the button image inside UIToolbar?
I am actually using this way but it doesn't work.
[myButton setImage:[UIImage imageNamed:#"blue.png"]];
For your information, "myButton" is an IBOulet UIBarButtonItem.
#property (nonatomic, strong) IBOutlet UIBarButtonItem *myButton;
Use the following cole. i hope it will use to you.
UIImage *image = [UIImage imageNamed:#"buttonImage.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[button setImage:image forState:UIControlStateNormal];
[button addTarget:myTarget action:#selector(myAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Thanks,.
// add images to bar button item of toolbar in iphone
to set button type as custum:
button=[UIButton buttonWithType: UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"myImage.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 40, 40)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
// add the image on toolbar:
[toolbar insertSubview:[[[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"image.png"]] autorelease] atIndex:1];
- (void)viewDidLoad
{
[super viewDidLoad];
//Load the image
UIImage *buttonImage = [UIImage imageNamed:#"1.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//sets the frame of the button to the size of the image
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
//creates a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
customBarItem.target = self;
customBarItem.action = #selector(click:);
self.toolbarItems = [NSArray arrayWithObjects:customBarItem, nil];
}
- (void)click:(id)sender {
NSLog(#"action");
}
I think my console will print "action" when I push down the barItem.
However "action" is not printed.
Did I miss anything?
Thanks for advance!
Try this
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:#"Click ME!" style:UIBarButtonItemStyleBordered target:self action:#selector(click)];
.
.
.
-(void)click{
NSLog("hello");
}
I believe the problem is that you are using a UIButton for the [[UIBarButtonItem alloc] initWithCustomView when you should simply pass a UIView, the action is probably going to the UIButton and not the UIBarButtonItem.
Instead do this:
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"1.png"];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:myImageView];
I think you're trying to do something more like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
[button setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[button addTarget:self action:#selector(click) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
UIBarButtonItem *doneitem=[[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(donePressed:)]autorelease];
self.navigationItem.rightBarButtonItem=doneitem;
This is the code of my app, I need to add a image on this button ?
Please help me.
Try this code:
UIImage* image3 = [UIImage imageNamed:#"mail-48_24.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:#selector(sendmail)
forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.rightBarButtonItem=mailbutton;
[someButton release];
UIBarButtonItem *_btn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"XXXXXXX.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(yourMethod)];
self.navigationItem.rightBarButtonItem=_btn;
Please try this code :
UIButton *btnNext1 =[[UIButton alloc] init];
[btnNext1 setBackgroundImage:[UIImage imageNamed:#"btnNext.png"] forState:UIControlStateNormal];
btnNext1.frame = CGRectMake(100, 100, 50, 30);
UIBarButtonItem *btnNext =[[UIBarButtonItem alloc] initWithCustomView:btnNext1];
[btnNext1 addTarget:self action:#selector(nextButtonClicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = btnNext;
UIButton *urButton = [UIButton buttonWithType:UIButtonTypeCustom];
urButton.frame = urRequiredFrame;
[urButton setImage:urImage forState:UIControlStateNormal];
[urButton addTarget:self action:#selector(donePressed:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *doneButton =[[UIBarButtonItem alloc] initWithCustomView:urButton];
self.navigationItem.rightBarButtonItem=doneButton;
In case anyone needs Swift code for accepted answer:
let infoImage = UIImage(named: "my-icon-32.png")
let imgWidth = infoImage?.size.width
let imgHeight = infoImage?.size.height
let button:UIButton = UIButton(frame: CGRect(x: 0,y: 0,width: imgWidth!, height: imgHeight!))
button.setBackgroundImage(infoImage, forState: .Normal)
button.addTarget(self, action: Selector("openInfo"), forControlEvents: UIControlEvents.TouchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
P.S. Gurpreet Singh answer is working only for transparent PNG also I have to set button tintcolor other than clearColor.
In Swift
Other solution using withRenderingMode(UIImage.RenderingMode.alwaysOriginal) method below:
let img = UIImage(named: "picture")!.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
let leftBarButtonItem = UIBarButtonItem(image: img, style: UIBarButtonItem.Style.plain, target: self, action: nil)
self.navigationItem.leftBarButtonItem = leftBarButtonItem
Here's what works for me. I found 30x30 to be a good size for the button in the nav bar. The UIImage scales to the button size automatically.
UIImage *image = [UIImage imageNamed:#"XXXXXXXXXX"];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(someAction) forControlEvents:UIControlEventTouchUpInside];
button.adjustsImageWhenHighlighted = NO;
UIBarButtonItem *rightButton =[[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = rightButton;
[doneItem setImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
One simple way :
UIImage* customImg = [UIImage imageNamed:#"custom-img.png"];
UIBarButtonItem *_customButton = [[UIBarButtonItem alloc] initWithImage:customImg style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:_customButton, nil];
Then if you need more than one button you can just and an other UIBarButtonItem to initWithObjects.
Do this. It's way more simple.
Put the image file in your project directory
Add the file into Xcode (Right click on the Xcode project, add files to "Project Name")
Select your UIBarButtonItem in Storyboard
Click on "Image" and find your image (See Screenshot)
Celebrate, because it will work perfectly and it requires no unnecessary code.
To pick up on the answer by Gurpreet Singh. To keep the current status of an already existing button re-use the target and action.
SEL selector = self.navigationItem.rightBarButtonItem.action;
id target = self.navigationItem.rightBarButtonItem.target;
UIBarButtonItem *_btn=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"icon_close.png"]
style:UIBarButtonItemStylePlain
target:target
action:selector];
self.navigationItem.rightBarButtonItem = _btn;
let sliderImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 30))
sliderImageView.contentMode = .ScaleAspectFit
sliderImageView.image = UIImage(named: "sliderMenu")
OR
var sliderImage = UIImage(named: "sliderMenu")
navigationItem.leftBarButtonItem?.image = sliderImage