Instagram-like refresh button - iphone

I'm really tried to do a button (rightBarButtonItem) like the instagram's button, I think is not possible to set background-image & image to the button in the same time.
See this in
**
AppDelegate.m
**
NSDictionary *faTextAttributes = #{ UITextAttributeFont : [UIFont fontWithName:#"Arial" size: 12],
UITextAttributeTextColor : [UIColor whiteColor],
UITextAttributeTextShadowColor: [UIColor blackColor],
UITextAttributeTextShadowOffset: [NSValue valueWithCGSize: CGSizeMake(0,0)] };
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setTitleTextAttributes:faTextAttributes forState:UIControlStateNormal];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setTitleTextAttributes:faTextAttributes forState:UIControlStateHighlighted];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setBackgroundImage:[UIImage imageNamed:#"bar"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setBackgroundImage:[UIImage imageNamed:#"bar"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
FirstViewController.m
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightButton;
Here the images:
Background image (2x) http://d.pr/i/BYS9
Refresh image (2x) http://d.pr/i/Hg5u
What im looking for to do the same button of instagram application, set the refresh image inside the background image.
Hope you help me about this.

Unfortunately UIBarButtonItem is not a subclass of UIButton and therefore does not have the complex properties and customizability that UIButton has. The way Instagram is accomplishing this button is to have only one image that is the Refresh image superimposed over the background image. When the button is pressed, they hide the item and replace it with pinwheel.
alternatively you could create a custom button, add your desired attributes to that button and then use that button to create the bar items:
UIBarButton* button initWithCustomView:customButton;

Related

iOS: changing the tint color of the back button?

I have a navigation controller that pushes a UIViewController. I would like to change the tint color of the back button of the navigation item when a user presses on a certain button. Is this possible? I have tried using [UIBarButtonItem appearance] setTintColor: but it only works on initialization (for example in viewDidLoad) and not otherwise.
Try this......
You need to use a UIBarButtonItem with a custom view. Something like this:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
[button addTarget:target action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:[UIImage imageNamed:#"back_button.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"back_button_tap.png"] forState:UIControlStateHighlighted];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
And put the button to the navigation bar, usually in a controller with UINavigationController:
self.navigationItem.leftBarButtonItem = buttonItem;
The easiest thing for me was to set the tint color for ALL uibarbutton items:
[[UIBarButtonItem appearance]setTintColor:[UIColor yourColor]];
And then explicitly setting the tintcolor for explicit navigationbuttons that I create & place on the navigationbar to other colors...
Saves me the headache of creating custom backbuttons when all I want to do is change the tint.
In the method that is called on your button click, just put [self.navigationItem.backBarButtonItem setTintColor:[UIColor *whateverColorYouWant*]]; I haven't tested it but I'm 99% sure that would work
Edit:
Just tested it, it works.
Here's a code that changes text and color of the back button:
- (void)viewDidLoad
{
UIBarButtonItem *backButton = [UIBarButtonItem new];
[backButton setTitle:#"Back"];
[backButton setTintColor:[UIColor yellowColor]];
[[self navigationItem] setBackBarButtonItem:backButton];
}
Try this:
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class],[UIToolbar class], nil] setTintColor:[UIColor redColor]];

Cant set image to backBarButtonItem

i have UITableView based application, and i can't set backBarButtonItem properly.
Back button is button, which appears when i select any item, call second view with method didSelectRowAtIndexPath. When i try to set it like this:
UIButton *back1=[UIButton buttonWithType:UIButtonTypeCustom];
[back1 setFrame:CGRectMake(10.0, 2.0, 45.0, 40.0)];
[back1 addTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
[back1 setImage:[UIImage imageNamed:#"back_button.png"] forState:UIControlStateNormal];
UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:back1];
self.navigationItem.backBarButtonItem = back;
There is just back button with default style and somehow with title of my TableView controller window.
When i set it like this:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"back_button.png"]
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
There is an image, but, it look like "inserted" in default back button, doesn't look nice at all.
Please help me.. i tried many ways to solve this, but, in many cases there is just default Back button with title "Products" (there is title which appears on top of navigation bar in my UITableView). Any help would be appreciated!
UIImage *buttonImage = [UIImage imageNamed:#"back_button.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
backButton.font = [UIFont boldSystemFontOfSize:12];
backButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[backButton addTarget:self action:#selector(showMeter)
forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:backButton];
//Create resizable images
UIImage *myButton = [[UIImage imageNamed:#"yourButton"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
//Set the background image for *all* UIBackBarButtons
[[UIBarButtonItem appearance] setBackgroundImage:myButton forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
You should assign it to self.navigationItem.leftBarButtonItem.
self.navigationItem.leftBarButtonItem = backButton;

how to change the color of backBarButtonItem?

In my application I want to change the color of bacBarButtonItem.
Is it possible to change the color? or I have to put an image of it.
and in case of image tell me the code how to put the image.
If you just want to change the color you can do it with this line of code.
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
Replace redColor with the following to adjust the color of the buttons:
colorWithRed:0/255.0 green:144/255.0 blue:200/255.0 alpha:1.0// pick your color using this
Be sure to put this in the view controller that pushes. Not the view controller where you want to see this back button color.
Praveen-K answer is right, but take in mind that you'll have to do it in every viewcontroller.
Starting at iOS5, Apple have introduced the "appearance" concept.
- (void)setBackButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;
In your case would be something like this
UIImage *image = [UIImage imageNamed:#"imageName.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
But as I said, Praveen-K answer is ok and will work, but just to let you know for the future.
UIImage *image = [UIImage imageNamed:#"imageName.png"];
UIBarButtonItem* backBarButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:#selector(backButtonAction)];
self.navigationItem.leftBarButtonItem=backBarButton;
[backBarButton release];
Another way to change the color of back bar button item is to use segment control
UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Back", nil]] autorelease];
button.frame = CGRectMake(0, 0, 60, 30);
button.center = self.view.center;
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = [UIColor colorWithRed:0 green:0.1 blue:0.5 alpha:0];
[button addTarget:self action:#selector(handleBack:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Notice that we assign the color we want to the property tintColor of UISegmentedControl.
I got the idea from this site:
http://charles.lescampeurs.org/2011/02/10/tint-color-uibutton-and-uibarbuttonitem

Is there an easy way to change the text color of a UIBarButtonItem without using an image?

Is there an easy way to change the text color of a UIBarButtonItem without using an UIImage as the background?
By default the text is always white. I'd like to make it black.
Thank to IOS 5.0 you do not need to use images for that.You can set various text attributes with following code.
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:#"Title" style:UIBarButtonItemStyleBordered target:nil action:nil];
if ([button respondsToSelector:#selector(setTitleTextAttributes:forState:)]) {
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
kTextColor,UITextAttributeTextColor,
nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:textAttributes
forState:UIControlStateNormal];
}
I'd rather use a UIButton inside a UIBarButtonItem and customize that one.
This is an example with custom graphics for a custom UIButton. The idea stays the same use initWithCustomView of the UIBarButtonItem to put something else in it which is easily customizable.
self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeButton setImage:[UIImage imageNamed:#"webview_close_button_normal.png"] forState:UIControlStateNormal];
[closeButton setImage:[UIImage imageNamed:#"webview_close_button_pressed.png"] forState:UIControlStateHighlighted];
closeButton.frame = CGRectMake(0, 0, 121, 36);
[closeButton addTarget:self action:#selector(closeAdViewController) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * aBarButtonAdClose = [[[UIBarButtonItem alloc] initWithCustomView:closeButton] autorelease];
You can set a custom view by using
[[UIBarButtonItem alloc] - (id)initWithCustomView:(UIView *)customView]
The direct answer to your question is no.
You can't simply change the color of a UIBarButtonItem by setting one of its properties. You can set the tintColor property of the UIToolBar or UINavigationBar that contains the UIBarButtonItems, but that also affects the color of the toolbar itself and doesn't offer too much customization.
If that doesn't work for you, a custom view as the other answers suggest is a fine idea.
Good luck!

How can I change the font color of a UIBarButton item?

I want to change the color to red.
I found a simpler way to just change the color of title:
iOS7:
UIBarButtonItem *button =
[[UIBarButtonItem alloc] initWithTitle:#"Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[button setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor], NSForegroundColorAttributeName,nil]
forState:UIControlStateNormal];
and prior iOS7:
UIBarButtonItem *button =
[[UIBarButtonItem alloc] initWithTitle:#"Title"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[button setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor], UITextAttributeTextColor,nil]
forState:UIControlStateNormal];
This question was answered here. Basically, you have to create a UIButton, configure it as you wish, and then initialize the Bar Button Item with the UIButton as a custom view.
There are Two ways to change the color of the Text displayed UIBarButtonITem.
1)
[barButtonITem setTintColor:[UIColor redColor]];
2) or you can you any UIColor class method. this is really efficient solution.Althought for iOS 7 you can use NSForegroundColorAttributeName and can use
barButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor clearColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
On similar note from Guvvy Ava you can change font size like
[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:#"Helvetica-Bold" size:26.0], UITextAttributeFont,nil] forState:UIControlStateNormal];
swift version 4.0 or later
barButtonITem.tintColor = UIColor.red // for textColor change
if want to change font as well as text color use the following code
barButtonITem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor :UIColor.red,NSAttributedStringKey.font:UIFont.init(name:"Avenir", size:15.0)], for: UIControlState.normal)
If you are using system Navigation bar button item then to change your button tint color use
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
To change button text color use the following code also.
[[UIBarButtonItem appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
Add the following line of code which viewcontroller you want to change your color