Centre vertically the title of the bar button iOS7 - ios

I need to centre vertically the title of the bar button and the button itself, as you can see from the code below, I tried to use setTitlePositionAdjustment and setBackgroundVerticalPositionAdjustment.
I expected:
setTitlePositionAdjustment to change the title position, however it
changed the position of my background image.
setBackgroundVerticalPositionAdjustment to change background image
position, however it didnt make any difference
I am a bit confused of this behaviour as the same code worked fine for iOS6. And I am looking for any solution to centre UIButtonItem title vertically. Thank you for your time and help in advance.
+(UIBarButtonItem *)barButtonItemWithTitle:(NSString *)title Color:(UIColor *)color Target:(id)target Action:(SEL)action{
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:target action:action];
[barButton setBackgroundImage:[UIImage imageNamed:#"standardButtonBackgroundHighlight"]];
[barButton setBackgroundVerticalPositionAdjustment:-2.0 forBarMetrics:UIBarMetricsDefault];
[barButton setTitlePositionAdjustment:UIOffsetMake(1, 2) forBarMetrics:UIBarMetricsDefault];
NSShadow *shadow = [[NSShadow alloc ]init];
shadow.shadowColor = [UIColor clearColor];
NSDictionary *attributes = #{ NSForegroundColorAttributeName : [UIColor redColor],
NSShadowAttributeName: shadow,
NSFontAttributeName : /*pass the font*/ };
[barButton setTitleTextAttributes:attributes forState:UIControlStateNormal];
return barButton;
}
Not using storyboard or nibs.

Try to play with the frame of UIButton as custom view for UIBarButtonItem.
*infoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[infoBtn addTarget:self action:#selector(infoClicked:) forControlEvents:UIControlEventTouchUpInside];
[infoBtn setImage:[UIImage imageNamed:#"btn.png"] forState:UIControlStateNormal];
[infoBtn setImage:[UIImage imageNamed:#"btn-selected.png"] forState:UIControlStateHighlighted];
[infoBtn setFrame:CGRectMake(0.f, 0.f, 60.f, 20.f)];
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc] initWithCustomView:barBtn];

Related

iOS - Change only backBarButtonItem text color on a navigation bar

I'm trying to change the color of the backBarButtonItem on a navigation bar in my app (iOS 9+).
I can do this with :
[[UIBarButtonItem appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
But this will change the color for all UIBarButtonItems on the navigation bar and I only want to change the back button.
I have tried :
[self.navigationItem.leftBarButtonItem setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
and
[self.navigationItem.leftBarButtonItem setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
But it doesn't work
NOTE: I want to keep the < arrow of the system back button so using a custom view is not an option unless I use a custom image
In the end I used the following solution:
1. Set the general appearance of UIBarbuttonitem BEFORE adding the right bar button item
[[UIBarButtonItem appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor blueColor]} forState:UIControlStateNormal];
2. Then set the right bar button item and its specific appearance
UIBarButtonItem *rigthBtn = [[UIBarButtonItem alloc] initWithTitle:#"title" style:UIBarButtonItemStylePlain target:self action:#selector(rightBtnTapped:)];
[rigthBtn setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
[self.navigationItem setRightBarButtonItem:rigthBtn];
As per my comment posting sample code
UIButton *useButton = [UIButton buttonWithType:UIButtonTypeCustom];
useButton.frame = CGRectMake(0, 0, 20,17);
useButton.layer.masksToBounds = NO;
useButton.layer.shadowOffset = CGSizeMake(3, 3);
useButton.layer.shadowRadius = 3;
useButton.layer.shadowOpacity = 0.1;
useButton.layer.shadowColor = [UIColor blackColor].CGColor;
useButton.backgroundColor = [UIColor clearColor];
useButton.tintColor = [UIColor whiteColor];
[useButton setImage:[UIImage imageNamed:#"image if any"] forState:0];
[useButton addTarget:self action:#selector(btnBackTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:useButton]];
`
One year too late but did you tried to set the tintColor? I dont know your syntax but in swift it would be:
navigationController.navigationBar.tintColor = UIColor.COLOR

Setting image for UINavigationitem (Leftbarbutton item) in ios

I m trying to setting image for UIBarButtonItem using the below code. But it appears as a blank image in the navigation bar. please help me to resolve the problem. i m entirely new to ios.
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:38.0/255.0 green:126.0/255.0 blue:202.0/255.0 alpha:0.5]];
UIBarButtonItem *lftbarbtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"Cross_Icon.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(additem)];
[self.navigationItem setLeftBarButtonItem:lftbarbtn];
thank u in advance.......
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(popToBack)]; //create the left bar button
self.navigationItem.leftBarButtonItem = leftBarButton; //assign into navigation item
self.navigationItem.leftBarButtonItem.tintColor=[UIColor colorWithRed:125.0/255.0 green:90.0/255.0 blue:146.0/255.0 alpha:1]; //set the tint color
[leftBarButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor purpleColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal]; //text color
-(void)popToBack
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
in another choice
UIButton *leftbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftbutton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[leftbutton addTarget:target action:#selector(buttonAction)forControlEvents:UIControlEventTouchUpInside];
[leftbutton setFrame:CGRectMake(0, 0, 53, 31)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(3, 5, 50, 20)];
[label setFont:[UIFont fontWithName:#"Arial-BoldMT" size:13]];
[label setText:title];
label.textAlignment = UITextAlignmentCenter;
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
[leftbutton addSubview:label];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView: leftbutton];
self.navigationItem.leftBarButtonItem = barButton;
-(void) buttonAction
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
You do not need to use the navigation bar directly. Use the current view controller's navigation item property to set the UIBarButtomItems. Make sure you are actually using a navigationController to set these properties.
UIButton *ReqMenuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
ReqMenuBtn.frame = CGRectMake(0,0,32,32);
[ReqMenuBtn setBackgroundImage:[UIImage imageNamed:#"reselect.png"] forState:UIControlStateNormal];
[ReqMenuBtn addTarget:self action:#selector(somemethodName:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *MenuButton = [[UIBarButtonItem alloc]initWithCustomView:ReqMenuBtn];
[self.navigationItem setLeftBarButtonItem:MenuButton];
MenuButton = nil;
ReqMenuBtn = nil;

How to change Bar Button Item colour?

In my detail view controller (part of navigation controller application) I've added custom "Back" button, like this:
- (void)loadView
{
[super loadView];
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 10.0f, 24.0f)];
UIImage *backImage = [UIImage imageNamed:#"left_arrow_icon"];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
[backButton setTitle:#"" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
}
-(void) popBack {
[self.navigationController popViewControllerAnimated:YES];
}
As you can see I added left bar button and assign "popBack" action. Basically left_arrow_icon is silver but when I press it, iOS change it darker grey.
My question is, can I (and how) change initial colour to white? Is that possible?
Edit. I use xcode5
Edit2:
That does't work too
Try this code. It works perfectly for me -
UIImageView *navigationBarImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Navigation.png"]];
navigationBarImage.frame = CGRectMake(0, 0, 320, 44);
navigationBarImage.userInteractionEnabled = YES;
navigationBarImage.backgroundColor = [UIColor colorWithRed:28.0/255.0 green:53.0/255.0 blue:102.0/255.0 alpha:1.0];
[self.view navigationBarImage];
[navigationBarImage release];
UIButton *backBarBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
backBarBtn1.frame = CGRectMake(13, 12, 20, 20);
[backBarBtn1 setImage:[UIImage imageNamed:#"ic_back3.png"] forState:UIControlStateNormal];
backBarBtn1.backgroundColor = [UIColor clearColor];
[backBarBtn1 addTarget:self action:#selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBarBtn1];
Try this code -
[backbutton setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:HELVETICA_REGULAR_FONT size:17.0], UITextAttributeFont, nil] forState:UIControlStateNormal];

Button overlapping when add uibutton in uinavigationbar in iOS

I have a problem when i change viewcontroller, uibutton added in uinavigationbar has been overlapped. I tried below code:
int submit_x = 170 ;
int refresh_x =100;
UIButton *refreshbtn=[UIButton buttonWithType:UIButtonTypeCustom];
UIImage *imageRefresh = [UIImage imageNamed:#"Refresh.png"];
[refreshbtn setImage:imageRefresh forState:UIControlStateNormal];
[refreshbtn setFrame:CGRectMake(refresh_x, 7, imageRefresh.size.width, imageRefresh.size.height)];
[refreshbtn addTarget:self action:#selector(refreshClicked:) forControlEvents:UIControlEventTouchUpInside];
refreshbtn.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar addSubview:refreshbtn];
I added above code in ViewDidAppear(). How can i fix this issue?
I believe you want to add UIBarButtonItem not UIButton to your navigation controller
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshClicked:)];
self.navigationController.navigationItem.rightBarButtonItem = refreshButton;
Try that and let me know is it what you after.
//EXTENDED
UIButton *refreshbtn=[UIButton buttonWithType:UIButtonTypeCustom];
UIImage *imageRefresh = [UIImage imageNamed:#"Refresh.png"];
[refreshbtn setImage:imageRefresh forState:UIControlStateNormal];
[refreshbtn setFrame:CGRectMake(refresh_x, 7, imageRefresh.size.width, imageRefresh.size.height)];
[refreshbtn addTarget:self action:#selector(refreshClicked:) forControlEvents:UIControlEventTouchUpInside];
refreshbtn.backgroundColor = [UIColor clearColor];
UIBarButtonItem *yourButton = [[UIBarButtonItem alloc] initWithCustomView: refreshbtn];
self.navigationItem.rightBarButtonItem = yourButton;
You should add UIButton directly to navigationcontroller.
//EXTENDED
Create another UIBarButtonItem (the same way as above) and add it like that:
self.navigationItem.rightBarButtonItems = #[yourButton, yourAnotherButton];
It should work fine with iOS6 and 7

How to change the title color of UIToolbar buttons?

Is there any easy method to change the title color of UIToolBar button title like what we are using for UIBarButtonItem:
[[UIBarButtonItem appearance]
setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],
UITextAttributeTextColor,nil]
forState:UIControlStateNormal];
UITextAttributeTextColor is deprecated in iOS >= 7.0.
Use setTintColor:(UIColor *) instead to change color of button's title.
Set default title color for UIToolBar:
[_myUIToolBar setTintColor:[UIColor redColor]];
or set title color of single UIBarButtonItem:
[_myUIBarButtonItem setTintColor:[UIColor blueColor]];
+(UIBarButtonItem*)barButtonItemWithTint:(UIColor*)color andTitle:(NSString*)itemTitle andTarget:(id)theTarget andSelector:(SEL)selector
{
UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:itemTitle, nil]] autorelease];
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = color;
[button addTarget:theTarget action:selector forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *removeButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
return removeButton;
}
used this code or http://fredandrandall.com/blog/2011/03/31/how-to-change-the-color-of-a-uibarbuttonitem/ check this link
Throughtout APP:
UIToolbar.appearance().barTintColor = TOOLBAR_BACKGROUND_COLOR
UIToolbar.appearance().tintColor = TOOLBAR_TITLE_COLOR
Also
if let font = UIFont(name: "AvenirNext-DemiBold", size: 15) {
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName:TOOLBAR_TITLE_COLOR], forState: UIControlState.Normal)
}
//Best Way.. Try with setting the images for UIBarButtonItem..
UIButton *a1 = [UIButton buttonWithType:UIButtonTypeCustom];
[a1 setFrame:CGRectMake(0.0f, 0.0f, 18.0f, 18.0f)];
[a1 addTarget:self action:#selector(BarButton_Clicked:) forControlEvents:UIControlEventTouchDown];
[a1 setImage:[UIImage imageNamed:#"UnSelected.png"] forState:UIControlStateNormal];
[a1 setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateSelected];
UIBarButtonItem *btn_Image = [[UIBarButtonItem alloc] initWithCustomView:a1];
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
[flexibleSpace setWidth:25.0];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:flexibleSpace,btn_Image,nil];
here the sample code:
[UIToolbar appearance]
Adding after your text attribute:
Here a UIToolBar class reference:
UIToolBar class

Resources