Customize navigation bar button in iOS6 - ios

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.

Related

How to change the position of buttons in uitoolbar in ios and set a badge for button to show the counts

I am having a toolbar and in that i have 3 buttons i gave the code as below
UIButton *likeImage = [UIButton buttonWithType:UIButtonTypeCustom];
[likeImage setFrame:CGRectMake(0, 0, 30, 30)];
[likeImage setBackgroundImage:[UIImage imageNamed:#"likeIconDefault"] forState:UIControlStateNormal];
[likeImage addTarget:self action:#selector(likeImagePressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *likeImageButton = [[UIBarButtonItem alloc] initWithCustomView:likeImage];
UIButton *commentImage = [UIButton buttonWithType:UIButtonTypeCustom];
[commentImage setFrame:CGRectMake(200, 0, 30, 30)];
[commentImage setBackgroundImage:[UIImage imageNamed:#"commentIcon"] forState:UIControlStateNormal];
[commentImage addTarget:self action:#selector(commentImagePressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *commentImageButton = [[UIBarButtonItem alloc] initWithCustomView:commentImage];
UIButton *dislikeImage = [UIButton buttonWithType:UIButtonTypeCustom];
NSArray *buttonItems = [NSArray arrayWithObjects: likeImageButton, commentImageButton, nil];
position of the button is not changing even after giving different values for X and Y coordinates

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.

Adding buttons to UIToolBar Programmatically

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.

UIBarButtonItem with image doesn't show

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

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

Resources