Adding buttons to UIToolBar Programmatically - ios

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.

Related

RightBar Button on navigation bar is not giving click like look

I want to show image and the title on RightBar Button so i have written code as below:
UIBarButtonItem *negativeSpacer = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
negativeSpacer.width = BARBUTTONITEM_PADDING_IPAD;
UIImage *buttonImage = [UIImage imageNamed:#"select-ipad#3x.png"];
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectButton setImage:buttonImage forState:UIControlStateNormal];
[selectButton setTitle:#"Select" forState:UIControlStateNormal];
[selectButton setTitleColor:[UIColor colorWithRed:0.76 green:0.21 blue:0.08 alpha:1.0] forState:UIControlStateNormal];
[selectButton.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:14.0]];
[selectButton sizeToFit];
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:selectButton];
[selectButton addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,aBarButtonItem,nil]];
This code shows the title and image on the NavigationBar and when i click on it, then the clicked like appears only on the image but not on the text.
-(void)viewWillAppear:(BOOL)animated{
UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[btn setImage:[UIImage imageNamed:#"select-ipad#3x.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(onBtnRightClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnRight=[[UIBarButtonItem alloc]initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = btnRight;
}
-(IBAction)onBtnRightClick:(id)sender
{
NSLog(#"Right Bar Button Clicked.");
}
Using your code, try 2 buttons:
UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(0.0, 0.0, 45.0, 45.0)];
[button1 addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageNamed:#"select-ipad#3x.png"] forState:UIControlStateNormal];
UIBarButtonItem *buttonItem1 = [[UIBarButtonItem alloc]initWithCustomView:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self action:#selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:#"Select" forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor colorWithRed:0.76 green:0.21 blue:0.08 alpha:1.0] forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[button2.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:14.0]];
[button2 sizeToFit];
UIBarButtonItem *buttonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
self.navigationItem.rightBarButtonItems = #[buttonItem2,buttonItem1];
I hope this can help you.

How to cancel UIBarbuttonItem setTitle shining effect

Like image shows, I used UIBarbuttonItem as rightBarButtonItem, what I want to do is just change rightBarButton's title when I select a picture or deselect a picture, so I used code like this:
[self.rightBarButton setTitle:[NSString stringWithFormat:#"完成(%#/%#)",#(_selectedAssets.count),#(self.maxPicturesNum)]];
but the barbutton will show shin when its text changed, I try to use UIButton instead of UIBarButtonItem, and shin effect disappear indeed, but UIButton will be close to right bounds far, can you help me?
You can create UIButton and assign it to your rightBarButton
UIButton *rightButton = [[UIButton alloc] initWithFrame:someFrame];
[rightButton setBackgroundImage:someImage forState:UIControlStateNormal];
[rightButton addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
rightButton.titleLabel.text = [NSString stringWithFormat:#"完成(%#/%#)",#(_selectedAssets.count),#(self.maxPicturesNum)];
rightButton.titleLabel.font = someFont;
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.rightBarButton = rightBarButtonItem;
And while updating label use rightButton.titleLabel.text
For Adjusting label use this:
- (void)setTitlePositionAdjustment:(UIOffset)adjustment
forBarMetrics:(UIBarMetrics)barMetrics
Finally,I do like this:
- (void)loadRightButtonItem{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(30, 0, 120, 40);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[button setTitle:#"完成(0/9)" forState:UIControlStateNormal];
button.tintColor = [UIColor whiteColor];
[button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
// [button setTitleColor:[self.buttonFolder titleColorForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
[button addTarget:self action:#selector(onFinishClicked:) forControlEvents:UIControlEventTouchUpInside];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 135, 40)];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:view];
[view addSubview:button];
self.navigationItem.rightBarButtonItem = item;
_rightBarButton = button;
}
It cancel the shin effect.
thanks to #RoHaN,your idea did great inspired me.

Customize navigation bar button in iOS6

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.

Custom background for default system items

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];

how to use custom UIBarButtonItem in interface whice all use custom UINavigationBar

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.

Resources