how to use custom UIBarButtonItem in interface whice all use custom UINavigationBar
Not written again in every interface.
Is there a unified method to uniform setting?
UIImage image = [UIImage imageNamed:imagePath];
UIButton button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:highLightImagePath] forState:UIControlStateHighlighted];
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
A lot of interface backBarButtonItem need custom,code is the same,I don't want to write N times
You can add this in a singleton global-variables file, like
GlobalVariable.h
+(UIBarButtonItem*)customButton;
GlobalVariable.m
+(UIBarButtonItem*)customButton{
UIImage *image = [UIImage imageNamed:imagePath];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:highLightImagePath] forState:UIControlStateHighlighted];
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* btn = [[UIBarButtonItem alloc] initWithCustomView:button];
return btn;
}
Then, import GlobalVariable.h in the views you want the button to appear and call
self.navigationItem.leftBarButtonItem = [GlobalVariable customButton];
You can keep GlobalVariable there, in case you need other variables too.
Related
I have added a toolbar and then added 3 buttons to it programmatically. However, the 3 buttons are all cluttered to a corner. What i want to do is to have even spaces between the 3 buttons so the UI look decent.
How can I do this?
UIButton *button = [UIButton buttonWithType:100];
[button addTarget:self action:#selector(hideGender:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"img"] forState:UIControlStateNormal];
[button setTitle:#"Images" forState:UIControlStateNormal];
button.imageEdgeInsets= UIEdgeInsetsMake(0.0, 0.0, 0, 50);
button.tag=0;
UIButton *button2 = [UIButton buttonWithType:100];
[button2 addTarget:self action:#selector(hideSchool:) forControlEvents:UIControlEventTouchUpInside];
button2.tag=2;
button2.imageEdgeInsets= UIEdgeInsetsMake(0.0, 0.0, 0, 50);
[button2 setImage:[UIImage imageNamed:#"img22"] forState:UIControlStateNormal];
UIButton *button3 = [UIButton buttonWithType:100];
[button3 addTarget:self action:#selector(hideCar:) forControlEvents:UIControlEventTouchUpInside];
button.tag=3;
button3.imageEdgeInsets= UIEdgeInsetsMake(0.0, 0.0, 0,50);
[button3 setImage:[UIImage imageNamed:#"img3"] forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem0 = [[UIBarButtonItem alloc] initWithCustomView:button] ;
UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2] ;
UIBarButtonItem *barButtonItem3 = [[UIBarButtonItem alloc] initWithCustomView:button3] ;
UPDATE: I want to use only UIbutton
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBackButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
[toolBar setItems:[NSArray arrayWithObject:barBackButton]];
Now in setItems set the array of buttons. Suppose you want to put three buttons, then width of each button will be screenWidth/3 and text will be center-aligned. In this way, there is no over-riding of button.
Update - One Possible Solution
Based on answer given by Alexander Merchi in this post (UIBarButtonItem Custom view in UINavigationBar), a solution is found. But still don't know how to change the position of the image properly.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 28.0f, 28.0f)]; // 28 points is the width or height of the button image
[btn setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(onClickMenuButton) forControlEvents:UIControlEventTouchUpInside];
btnMenu = [[UIBarButtonItem alloc] initWithCustomView:btn];
Original Post
In iOS 6, the goal is like this:
while current result is like this:
My code is
btnMenu = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"button.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(onClickMenuButton)];
button.png is this the white circle with white three bars inside and a tranparent background.
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 30, 30);
[rightButton setImage:[UIImage imageNamed:#"Button-normal"] forState:UIControlStateNormal];
[rightButton setImage:[UIImage imageNamed:#"logout-hover"] forState:UIControlStateHighlighted];
[rightButton addTarget:self action:#selector(logOut) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.leftBarButtonItem = rightBarButtonItem;
I am accomplishing it by
UIImageView *uView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"btn_back_ontouch_scr11.png"]];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.bounds = CGRectMake( 0, 0, uView.frame.size.width, uView.frame.size.height );
[leftButton setImage:uView.image forState:UIControlStateHighlighted];
[leftButton setImage:[UIImage imageNamed:#"btn_back_normal_scr11.png"]forState:UIControlStateNormal];
[leftButton addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
self.scrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite;
self.navigationItem.leftBarButtonItem = aButton;
self.navigationItem.hidesBackButton = YES;
This should work.
I am trying to add custom back button on navigation bar but it is not working below is my code
-(void)addLeftButton
{
UIImage *buttonImage = [UIImage imageNamed:#"btn_back.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.backBarButtonItem = aBarButtonItem;
}
Please tell what's wrong with this code
Try this.
-(void)addLeftButton
{
UIImage *buttonImage = [UIImage imageNamed:#"btn_back.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setLeftBarButtonItem:aBarButtonItem];
}
Here is the code that i have used to create custom backbutton
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the custom back button
//add the resizable image
UIImage *buttonImage = [[UIImage imageNamed:#"backbtn.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
MSLabel *lbl = [[MSLabel alloc] initWithFrame:CGRectMake(12, 4, 65, 28)];
lbl.text = #"Settings";
lbl.backgroundColor = [UIColor clearColor];
//lbl.font = [UIFont fontWithName:#"Helvetica" size:12.0];
lbl.numberOfLines = 0;
lbl.lineHeight = 12;
lbl.verticalAlignment = MSLabelVerticalAlignmentMiddle;
lbl.font = [UIFont fontWithName:#"Helvetica" size:12];
[button addSubview:lbl];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 70, buttonImage.size.height);
[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;
}
-(void)back {
// Tell the controller to go back
[self.navigationController popViewControllerAnimated:YES];
}
This code will resize your image so it can fit the text.
You can also put this button in any method.
You didn't add action to your button. So you need to add action to your button. For more info see in the below code..
-(void)addLeftButton
{
UIImage *buttonImage = [UIImage imageNamed:#"btn_back.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:#selector(backBtnPressed) forControlEvents:UIControlEventTouchUpInside]; // -------- Edit here -----
self.navigationItem.backBarButtonItem = aBarButtonItem;
}
-(void)backBtnPressed
{
[self.navigationController popViewControllerAnimated:YES];
}
I need to make custom "Settings" button in navigation bar.
But, unfortunately, I was unable to make in in storyboard editor correctly.
I need to see only my picture, no button rectangle.
Is there are a way to make with kind of button correctly?
Thanks.
You can add this code in the ViewController -viewDidLoad method:
for the left bar button:
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"NavBarMapBtn.png"];
[leftButton setImage:buttonImage forState:UIControlStateNormal];
leftButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[leftButton addTarget:self action:#selector(changeView) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
or for the right bar button:
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"NavBarMapBtn.png"];
[rightButton setImage:buttonImage forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[rightButton addTarget:self action:#selector(changeView) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
There's no need to add a BarButtonItem to your view in the xib or storyboard.
Here's what it could look like:
i want to use the default bar button items like
UIBarButtonItem *editFavoritesButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(startEndEditing:)];
The problem is that i want to change the background of these button but
[editFavoritesButton setBackgroundImage:[UIImage imageNamed:#"barbuttonPlain.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
does not to the trick in this case.
UPDATED:
The idea is not creating a custom button since you need to handle the translations for this case.On the other hand i want to user compose and other sort of default images.
Replace your code from...
[editFavoritesButton setBackgroundImage:[UIImage imageNamed:#"barbuttonPlain.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
as like this...
[editFavoritesButton setImage:[UIImage imageNamed:#"barbuttonPlain.png"]];
Use this code to create custom UIBarButtonItem:
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
[mybutton setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[mybutton addTarget:self action:#selector(mybuttonFunction) forControlEvents:UIControlEventTouchUpInside];
[mybutton setImage:[UIImage imageNamed:#"myImage.png"] forState:UIControlStateNormal];
UIBarButtonItem *random = [[UIBarButtonItem alloc] initWithCustomView:mybutton];
Hope this will help you.
You can try like this If you have any question you can ask..
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 83, 44);
backButton.adjustsImageWhenHighlighted=NO;
[backButton addTarget:self action:#selector(backMethod)
forControlEvents:UIControlEventTouchDown];
[backButton setImage:[UIImage imageNamed:#"img.png"]
forState:UIControlStateNormal];
UIBarButtonItem *barLeftInfoButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = barLeftInfoButton;
Add a custom button first and then add it to the UIBarButtonItem.
UIImage *buttonImage = [UIImage imageNamed:#"mainButton.png"];
//create the button and assign the image
self.yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.yourButton setFrame:CGRectMake(5,7,buttonImage.size.width,buttonImage.size.height)];
[self.yourButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
self.yourButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[self.yourButton setTitle:#"title" forState:UIControlStateNormal];
[self.yourButton addTarget:self action:#selector(startEndEditing:) forControlEvents:UIControlEventTouchUpInside];
// add image view
[self.navBar addSubview:self.yourButton];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:self.yourButton];
self.navigationItem.leftBarButtonItem = customBarItem;
Please try to use this one ....
UIButton *editFavoritesButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 55.0f, 30.0f)];
[editFavoritesButton setBackgroundColor:[UIColor clearColor]];
[editFavoritesButton setBackgroundImage:[UIImage imageNamed:#"barbuttonPlain.png"] forState:UIControlStateNormal];
[editFavoritesButton addTarget:self action:#selector(startEndEditing:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:editFavoritesButton];
[self.navigationItem setLeftBarButtonItem:leftBtn];