I wanted to create a barbutton that was a custom image so I created a subclass UIBarButtonItem.
#implementation UIBar
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action{
UIImage *menuButtonImage = [UIImage imageNamed:#"Camera.jpg"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:menuButtonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, menuButtonImage.size.width, menuButtonImage.size.height);
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[button addTarget:self action:#selector(takePicture) forControlEvents:UIControlEventTouchUpInside];
// self->navigationItem.rightBarButtonItem = menuButton;
[UIViewController release];
}
But the error I'm getting on the line:
self.navigationItem.rightBarButtonItem = button;
incomplete definition of type struct objc_class
Does this error have anything to do with my class not being initialised properly? I'm new to Objective-C.
Thanks for your help.
In your category method, you are not returning any UIBarButtonItem instance. Try to return the menuButton.
And I am not sure why you need to call class method of UIViewController. The method is for instance, not a class method. Remove that line too.
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action{
UIImage *menuButtonImage = [UIImage imageNamed:#"Camera.jpg"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:menuButtonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, menuButtonImage.size.width, menuButtonImage.size.height);
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[button addTarget:self action:#selector(takePicture) forControlEvents:UIControlEventTouchUpInside];
return menuButton;
}
If you want custom bar button then you can use the following code in your viewDidLoad method of viewController.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action: #selector(takePicture)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
If you want to subclass UIBarButtonItem then you can do the following.
#implementation UIBar
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return menuButton;
}
and in your viewController viewDidLoad method do the following
UIBar *customBarItem = [UIBar barItemWithImage:buttonImage target:self action:#selector(takePicture)];
self.navigationItem.leftBarButtonItem = customBarItem;
Both the method will work.
Related
I have added custom UIbarButtonItems on toolbar. Everything is created through NIB file. I see the images are stretched. My images are 20x20 and 40x40 for retina. They are png files and I ensured they dont contain any drop shadow and use anti-aliasing. Currently the toolbar looks like this.
If you see, the same images appear sharp when not added as a barbuttonItem but just as imageview. How can I fix this.
Could you try to create your UIBarButtonItems in your viewDidLoad method?
Here is how I do with a UINavigationBar and all images looks fine:
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *rightButtonImage = [UIImage imageNamed:#"img1.png"];
[rightButton setImage:rightButtonImage forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0.0, 7.0, rightButtonImage.size.width, rightButtonImage.size.height);
[rightButton addTarget:self action:#selector(method1) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightButton] autorelease];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *leftButtonImage = [UIImage imageNamed:"img2.png"];
[leftButton setImage:leftButtonImage forState:UIControlStateNormal];
leftButton.frame = CGRectMake(10.0, 7.0, leftButtonImage.size.width, leftButtonImage.size.height);
[leftButton addTarget:self action:#selector(method2) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];
method1 and method2 are void methods: -(void)method
and to add 2 buttons on the right side, I do something like this:
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *rightButtonImage = [UIImage imageNamed:#"img3"];
[rightButton setImage:rightButtonImage forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0.0, 7.0, rightButtonImage.size.width, rightButtonImage.size.height);
[rightButton addTarget:self action:#selector(method3) forControlEvents:UIControlEventTouchUpInside];
UIButton *middleRightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *middleRightButtonImage = [UIImage imageNamed:#"img4"];
[middleRightButton setImage:middleRightButtonImage forState:UIControlStateNormal];
middleRightButton.frame = CGRectMake(0.0, 7.0, middleRightButtonImage.size.width, middleRightButtonImage.size.height);
[middleRightButton addTarget:self action:#selector(method4) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:[[[UIBarButtonItem alloc] initWithCustomView:rightButton] autorelease], [[[UIBarButtonItem alloc] initWithCustomView:middleRightButton] autorelease], nil];
For UIToolbar you have to add items like this:
[toolbar setItems:[NSArray arrayWithObject:item1, item2, item3, item4, nil]];
I want to change the backbutton appearance for my backbutton in my navigation bar. For that I have this piece of code.
UIImage *backButtonImage = [UIImage imageNamed:#"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
backbutton.backgroundColor = [UIColor colorWithPatternImage:backButtonImage];
backbutton.titleLabel.text = #"back";
backbutton.titleLabel.textColor= [UIColor colorWithRed:50/255.0
green:158/255.0
blue:218/255.0
alpha:1.0];
backbutton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[backbutton addTarget:self action:#selector(back_Clicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = back;
But still I get the standard back button. Any help ?
Put your code to customize back button before pushing that viewController to the stack. And set BackBarButtonItem , not leftBarButtonItem.
UIImage *image = [UIImage imageNamed:#"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
backbutton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[backbutton setImage:image forState:UIControlStateNormal];
backbutton.titleLabel.textColor = [UIColor blueColor];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[[self navigationItem] setBackBarButtonItem: back];
[self.navigationController pushViewController:yourViewControllerObject animated:YES];
using bellow code for your requirement..
- (void)viewDidLoad
{
///write your code
UIImage *backButtonImage = [UIImage imageNamed:#"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
//[backbutton setImage:backButtonImage forState:UIControlStateNormal];//this line for set only image on button
[backbutton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[backbutton setTitle:#"Back" forState:UIControlStateNormal];
backbutton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[backbutton addTarget:self action:#selector(back_Clicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = back;
}
when back button clicked bellow method call and you will go to previous viewcontroller
-(void)back_Clicked{
[self.navigationController popViewControllerAnimated:YES];
}
Use
self.navigationItem.leftBarButtonItem = back;
Instead of
self.navigationController.navigationItem.leftBarButtonItem = back;
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:[self buttonWithImage:#"backButton" heighlightImageName:#"backButton" buttonFrame:CGRectMake(2, 1, 30, 30) selectorName:#selector(buttonBackClicked:) target:self]]];
- (UIButton*)buttonWithImage:(NSString*)normalImageName heighlightImageName:(NSString*)heighlightImageName buttonFrame:(CGRect)buttonFrame selectorName:(SEL)selectorName target:(id)target
{
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[tempButton setFrame:buttonFrame];
[tempButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside];
[tempButton setImage:[self getImageFromResource:normalImageName] forState:UIControlStateNormal];
[tempButton setImage:[self getImageFromResource:heighlightImageName] forState:UIControlStateHighlighted];
return tempButton;
}
- (UIImage*)getImageFromResource:(NSString*)imageName
{
return [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:#"png"]];
}
IButton *favButton = [[UIButton alloc] init];
[favButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[favButton addTarget:self action:#selector(favouriteButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithCustomView:favButton];
self.navigationItem.rightBarButtonItem = button;
[button release];
[favButton release];
I'm trying for hours to make a left button work properly and mimic a back button.
My code to create the button:
UIBarButtonItem *backButton = [self customBarButton:#"back_button" imageHiglighted:#"settings_button_highlighted" x:20 y:0 widthDivider:2.6 heightDivider:2.6];
backButton.target = self;
backButton.action = #selector(buttonPressed:);
self.navigationItem.leftBarButtonItem = backButton;
Here the method called to create custom button:
- (UIBarButtonItem *)customBarButton:(NSString *)imageDefault imageHiglighted:(NSString *)imageHighlighted x:(float)x y:(float)y widthDivider:(float)widthDivider heightDivider:(float)heightDivider {
UIImage *customImageDefault = [UIImage imageNamed:imageDefault];
UIImage *customImageHighlighted = [UIImage imageNamed:imageHighlighted];
CGRect frameCustomButton = CGRectMake(x, y, customImageDefault.size.width/widthDivider, customImageDefault.size.height/heightDivider);
UIButton *customButton = [[UIButton alloc] initWithFrame:frameCustomButton];
[customButton setBackgroundImage:customImageDefault forState:UIControlStateNormal];
[customButton setBackgroundImage:customImageHighlighted forState:UIControlStateHighlighted];
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];
return barCustomButton;
}
And the action:
-(void)buttonPressed:(id) sender{
NSLog(#"Entered");
SearchViewController *ViewController = [[SearchViewController alloc] init];
[self.navigationController pushViewController:ViewController animated:YES];
}
So I was able to make it with a simple UIButton but not with a UIButtonBarItem and I really don't know what's going on with it.
If you could help me I'd be very grateful.
Thanks.
Do this add selector to custom button as it is view of bar buttom:
[customButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
EDIT : Note : the target and action of the UIBarButtonItem apply to custom views.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"goback.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(//HERE ! i don't know put what thing !) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
TRY THIS
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[GlobalMethods buttonWithImage:#"top-nav-back" heighlightImageName:nil buttonFrame:CGRectMake(102, 1, 50, 30) selectorName:#selector(btnBackClicked:) target:self]];
+ (UIButton*)buttonWithImage:(NSString*)normalImageName heighlightImageName:(NSString*)heighlightImageName buttonFrame:(CGRect)buttonFrame selectorName:(SEL)selectorName target:(id)target
{
UIButton *objButton = [UIButton buttonWithType:UIButtonTypeCustom];
[objButton setFrame:buttonFrame];
[objButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside];
[objButton setImage:[GlobalMethods getImageFromResourceBundle:normalImageName] forState:UIControlStateNormal];
if(heighlightImageName)
[objButton setImage:[GlobalMethods getImageFromResourceBundle:heighlightImageName] forState:UIControlStateDisabled];
if(heighlightImageName)
[objButton setImage:[GlobalMethods getImageFromResourceBundle:heighlightImageName] forState:UIControlStateSelected];
return objButton;
}
Try this..
[customButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
in your customBarButton method..
I mean to say give selector to your button instead of bar button item..
Check and reply..
Just put this line in your code..
[backButton addTarget:self action:#selector(backBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
This worked for me
UIButton * tmpButton = [self generateButtonwithImageName:#"Back.png" andSize:CGRectMake(0, 0, 55, 30)];
UIBarButtonItem * backButton = [[UIBarButtonItem alloc] initWithCustomView:tmpButton];
[tmpButton addTarget:self action:#selector(reload) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem=backButton;
-(UIButton*)generateButtonwithImageName :(NSString*)imageName andSize:(CGRect)rect{
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=rect;
[button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
return button;
}
//working for me
if([version floatValue] < 7.0)
{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navi-bar.png"] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.opaque = YES;
closeButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 35, 30)];
[closeButton setImage:[UIImage imageNamed:#"btn_end.png"] forState:UIControlStateNormal];
[closeButton setImage:[UIImage imageNamed:#"btn_end_on.png"] forState:UIControlStateHighlighted];
[closeButton addTarget:self action:#selector(actionClose) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:closeButton];
}
else{
/*add button back to navigation*//*add buttom back to navigation*/
UIBarButtonItem *itemBack = [[UIBarButtonItem alloc]initWithTitle:#"完了" style:UIBarButtonItemStylePlain target:self action:#selector(actionClose)];
self.navigationItem.rightBarButtonItem = itemBack;
}
I have this code that changes the back button of my UINavigationBar
// Set the custom back button
UIImage *buttonImage = [UIImage imageNamed:#"backag.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"selback.png"] forState:UIControlStateHighlighted];
button.adjustsImageWhenDisabled = NO;
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 30, 30);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
// Cleanup
[customBarItem release];
If I put it in the viewDidLoad method it works fine. However, when I load the next view the old style button shows up. To fix this I tried putting this code in the next view's viewDidLoad method, but then no button is visible.
Any ideas as to what might be causing this?
Thanks!
Apply same code in
-(void)viewDidAppear:(BOOL)animated
{
}
I used tis code in both view and it's worked fine...
// add following code in your both view's viewDidLoad method...
UIButton *leftButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton1 setImage:[UIImage imageNamed:#"Viewone.png"] forState:UIControlStateNormal];
leftButton1.frame = CGRectMake(0, 0, 30, 30);
[leftButton1 addTarget:self action:#selector(yourclickevent) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton1];
[leftButton1 release];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setImage:[UIImage imageNamed:#"ViewSecond.png"] forState:UIControlStateNormal];
leftButton.frame = CGRectMake(0, 0, 30, 30);
[leftButton addTarget:self action:#selector(yourclickevent) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
[leftButton release];
Hope,this will help you...enjoy..
You need to ensure you set the hidesBackButton property on the navigationItem:
// Setup custom back button
self.navigationItem.hidesBackButton = YES;
I can't seem to add an image to this UIBarButtonItem without it crashing when touched:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
this code adds an image, but it crashes when touched because of an incompatible pointer types assigning to UIBarButtonItem * from UIButton:
//Setup Custom Button
UIButton *button = [[UIButton alloc] init];
button.frame = CGRectMake(0,0,44,44);
// [button addTarget:self action:#selector(showInfo) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.backBarButtonItem = button;
[button setBackgroundImage:imgIcon forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
[button release];
thanks for any help
To call
self.navigationItem.leftBarButtonItem = [CustomBarButton createNavBackBarButtonItemWithTitle:NSLocalizedString(#"Back", #"") target:self action:#selector(actionBack:)];
CustomBarButton has createNavBackBarButtonItemWithTitle class function
+(UIBarButtonItem *)createNavBackBarButtonItemWithTitle:(NSString *)t target:(id)tgt action:(SEL)a
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// Since the buttons can be any width we use a thin image with a stretchable center point
UIImage *buttonImage = [[UIImage imageNamed:#"back_button_up.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
UIImage *buttonPressedImage = [[UIImage imageNamed:#"back_button_over.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
[[button titleLabel] setFont:[UIFont boldSystemFontOfSize:12.0]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
//[[button titleLabel] setShadowOffset:CGSizeMake(0.0, 1.0)];
CGRect buttonFrame = [button frame];
buttonFrame.size.width = [t sizeWithFont:[UIFont boldSystemFontOfSize:12.0]].width + 24.0;
buttonFrame.size.height = buttonImage.size.height;
[button setFrame:buttonFrame];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];
[button setTitle:[NSString stringWithFormat:#" %#",t] forState:UIControlStateNormal];
[button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
return [buttonItem autorelease];
}
You have to set the buttonStyle to custom not a UIBarButtonItemStylePlain
and then [barbuttonitem setCustomView:anImageView];