Change background of a uinavigation right button? - ios

I am successful in changing the text of the right navigation button by given code. But i also want to change it has a delfault background .
I simply want to show a text with transparent background.
I have also tried
UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc] initWithTitle:#"MyTitle" style:UIBarButtonItemStyleDone target:self action:#selector(myMethod)];
rightBtn.tintColor =[UIColor clearColor];
but still showing a button background.
Is there a way to achieve it by changing any different style which don't have background. or is there an other simple way to change button to a text with no transparent background.

You can use this:
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
btn.backgroundColor=[UIColor clearColor];
[btn setTitle:#"Title" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(myMethod) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBtn=[[UIBarButtonItem alloc] initWithCustomView:btn];
[self.navigationItem setRightBarButtonItem:rightBtn];

Add this in viewDidLoad:
[self.navigationItem.rightBarButtonItem setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

Related

Changing vertical position of UIBarButtonItem on iOS 8.2

Here is my navigation bar
I had a problem about changing vertical position of Settings UIBarButtonItem on my navigation bar. I would like to move the button item "Settings" down
Here is my code
UIBarButtonItem *settingsItem = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStylePlain target:self action:nil];
self.navItem.rightBarButtonItem = settingsItem;
[[UIBarButtonItem appearance] setTitlePositionAdjustment:UIOffsetMake(0,-10) forBarMetrics:UIBarMetricsDefault];
I had tried it again and again. It seemed that it's not work
Could anyone suggest me how to move the button item "Settings" down ?
You can easily add a Custom Button to your NavigationBarItem, Here is the way,
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; //create custom Button
[button setTitle:#"Settings" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont systemFontOfSize:14.0]];
[button setTitleColor:[UIColor colorWithRed:179.0/255.0 green:40.0/255.0 blue:18.0/255.0 alpha:1] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 50, 20); //Button frame
[button addTarget:self action:#selector(yourCustomSelectorHere) forControlEvents:UIControlEventTouchUpInside]; //Add action method to the button here
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 63, 33)];
backButtonView.bounds = CGRectOffset(backButtonView.bounds, -14, -7);
[backButtonView addSubview:button];
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithCustomView: backButtonView]; //set button as UIBarButtonItem
self.navigationItem.rightBarButtonItem = barBtnItem; //set barBtnItem to rightBarButtonItem
By changing backButtonView.bounds values you can change the origin of the Button
Cited from apple's documentation on UIAppearance protocol
iOS applies appearance changes when a view enters a window, it doesn’t
change the appearance of a view that’s already in a window.
So if your code of setting UIBarButtonItem's appearance is after the navigationItem assignment code. It won't work

Can't See Custom uibarbuttonitem at Right side of navigation bar

I am trying to add custom navigation bar button at right side of navigation bar.
here is my code.
UIButton *customPrintButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customPrintButton setFrame:CGRectMake(0, 0, 100, 30)];
[customPrintButton.titleLabel setTextColor:[appDelegate getAppPinkColor]];
[customPrintButton setTitle:#"Print" forState:UIControlStateNormal];
[customPrintButton setBackgroundColor:[UIColor clearColor]];
[customPrintButton setExclusiveTouch:YES];
[customPrintButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
[customPrintButton.titleLabel setFont:[UIFont systemFontOfSize:20]];
[customPrintButton addTarget:self action:#selector(printOutButton_Clicked) forControlEvents:UIControlEventTouchUpInside];
printOutBtn = [[UIBarButtonItem alloc]initWithCustomView:customPrintButton];
self.navigationItem.rightBarButtonItem = printOutBtn;
but still i can't see the button on my page..
I forgot to add title color..
that's why default it takes white color as text color
Thanks for reply.

Centre vertically the title of the bar button iOS7

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

UIBarButtomItem image highlighted

I'm trying to setup a different image (highlighted) when the user press the UIBarButtomItem with this code:
self.addButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"addButton"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(addAlert:)];
[self.addButton setBackgroundImage:[UIImage imageNamed:#"addButtonHigh"]
forState:UIControlStateSelected
barMetrics:UIBarMetricsDefault];
self.navigationItem.rightBarButtonItem = self.addButton;
But it is not working.
The button appears with the "addButton" image, but when it is pressed the "addButtonHigh" image doesn't appear.
Thank you in advance,
Victor
change UIControlState from UIControlStateSelected to UIControlStateHighlighted. If you want to change the background image highlighted. You need to change the UIControlState.
The following is the code snippet i test. it works.
self.addButton = [[UIBarButtonItem alloc] initWithTitle:#"hello" style:UIBarButtonItemStylePlain target:self action:#selector(addAlert:)];
[self.addButton setBackgroundImage:[UIImage imageNamed:#"font_minus_32.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.addButton setBackgroundImage:[UIImage imageNamed:#"font_plus_32.png"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
self.navigationItem.rightBarButtonItem = self.addButton;
Also maybe the following code is the code you wanted.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:#"font_minus_32.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"font_plus_32.png"] forState:UIControlStateHighlighted];
[btn addTarget:self action:#selector(addAlert:) forControlEvents:UIControlEventTouchUpInside];
[btn sizeToFit];
self.addButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = self.addButton;
- (IBAction)buttonClicked:(id)sender
{
UIImage *buttonImage = [UIImage imageNamed:#"home.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateHighlighted];
}
UIControlStateHighlighted Highlighted state of a control. A control enters this state when a touch enters and exits during tracking and when there is a touch up event. You can retrieve and set this value through the highlighted property.
UIControlStateSelected Selected state of a control. For many controls, this state has no effect on behavior or appearance. But other subclasses (for example, the UISegmentedControl class) may have different appearance depending on their selected state. You can retrieve and set this value through the selected property.

changing text of custom navigation bar item

I am creating a custom UINavigationBar BarButtonItem as follows:
// Set up bar button items
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35.0f, 35.0f)];
[backButton setImage:[UIImage imageNamed:#"myimg.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(myselector) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
This is working perfectly, but when I go to set the title of the UIButton or BarButtonItem, it won't show up. Any suggestions?
Tried...
[backButton setTitle:#"Edit" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
And...
self.navigationItem.leftBarButtonItem.title = #"Edit";
without luck.
The problem is that image and title use same viewspace for display.
So when we use image with title first it show image and than title (right side of image).
If size of UIButton is same as image than we can't see Title.
A simple solution is set image in background. So try this:
[backButton setBackgroundImage:[UIImage imageNamed:#"myimg.png"] forState:UIControlStateNormal];
UIImage *image = [UIImage imageNamed:#"myimg.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:image forState:UIControlStateNormal];
[backButton setTitle:#"Edit" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]
backButton.frame=CGRectMake(0, 0, 35.0f, 35.0f);
UIBarButtonItem *barButton= [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
self.toolbar.items = [NSArray arrayWithObject:barButton];

Resources