How to use animated Navigation bar button in Objective c - objective-c

I want to add this Image on my Navigation Bar button, How can I use this image on my navigation Right Bar button or Left bar button?
Please check below code those I am using to View same like work code on Navigation bar button:-
- (void)viewDidLoad
{
[super viewDidLoad];
// Animation
UIImageView*animationView = [[UIImageView alloc] initWithFrame:self.view.frame];
animationView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.gif"],
[UIImage imageNamed:#"2.gif"],
[UIImage imageNamed:#"3.gif"],
[UIImage imageNamed:#"4.gif"],
[UIImage imageNamed:#"5.gif"],
[UIImage imageNamed:#"6.gif"],nil];
animationView.animationDuration = 1.25;
animationView.animationRepeatCount = 0;
[animationView startAnimating];
[self.view addSubview:animationView];
}
Let me know How
to use this code on navigation bar button.
Thank You!

I have tried your code, with a little trick of mine and it work like magic.
NSArray *imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"tmp-0"],
[UIImage imageNamed:#"tmp-1"],
[UIImage imageNamed:#"tmp-2"],
[UIImage imageNamed:#"tmp-3"],
[UIImage imageNamed:#"tmp-4"],
[UIImage imageNamed:#"tmp-5"],
[UIImage imageNamed:#"tmp-6"],
[UIImage imageNamed:#"tmp-7"],nil];
UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barButton setImage:[UIImage imageNamed:#"tmp-0"] forState:UIControlStateNormal]; // mine trick
[barButton.imageView setAnimationImages:imageArray];
[barButton.imageView setAnimationDuration:1.0f];
[barButton.imageView startAnimating];
[barButton sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:barButton];
You need to give a image to barButton at first, so your button can make its frame

Related

UINavigationItem rightbarbuttonitem not displaying

As my title, I have went through several websites and failed to achieve the result. However, I managed to add an image on the navigation bar successfully. These are two problems I'm facing currently, both are under UINavigationBar section.
1. Unable to display the rightbarbutton
2. Unable to hide the back arrow "<"
MasterViewController.m
//Back Button
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"sidemenu.png"];
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
backBtn.frame = CGRectMake(0, 0, 54, 30);
//Rightbarbutton
UIButton *infoButton = (UIButton *) [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon_info.png"]];
UIBarButtonItem *infoButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
self.navigationItem.rightBarButtonItem = infoButtonItem;
//Image (Working and displaying)
UIImage *image = [UIImage imageNamed: #"icon_image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(245, 0, 30, 42)];
[imageView setImage:image];
[self.navigationController.navigationBar addSubview:imageView];
I think you should initialize you rightItem's content as an UIButton and not a cast from ImageView to UIButton... so the same procedure you are doing for the back item..
This is your UINavigationItem+AtAdditions.h file
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, ATNavigationItemButtonSide) {
ATNavigationItemButtonSideRight,
ATNavigationItemButtonSideLeft
};
#interface UINavigationItem(ATAdditions)
// This method adds an image-only button to the requested size (right or left) of the current navigation item, the image can't be tapped
- (void)addColorfulImage:(NSString*)imageName toSide:(ATNavigationItemButtonSide)side;
#end
This is your UINavigationItem+AtAdditions.m file
#import "UINavigationItem+ATAdditions.h"
#implementation UINavigationItem(ATAdditions)
- (void)addColorfulImage:(NSString*)imageName toSide:(ATNavigationItemButtonSide)side {
// Create a button with the required image on it
UIImage *image = [UIImage imageNamed:imageName];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0, 0, image.size.width, image.size.height );
[button setImage:image forState:UIControlStateNormal];
// Stop the events (it's an image only)
button.userInteractionEnabled = NO;
// Add the button to the navigation item
if (side == ATNavigationItemButtonSideLeft) {
self.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}
else if (side == ATNavigationItemButtonSideRight) {
self.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}
}
#end
Later you can import the .h file and use it on the navigation item in your view controller.

How can I Use Animation On Navigation Right bar button?

I am using below code for Navigation right bar button:--
UIButton *settingsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[settingsBtn setImage:[UIImage imageNamed:#"logo_soft.png"] forState:UIControlStateNormal];
//[settingsBtn addTarget:self action:#selector(settings) forControlEvents:UIControlEventTouchUpInside];
[settingsBtn setFrame:CGRectMake(-16, -6, 95, 43)];
UIView *rightBarButtonItems = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 76, 32)];
//[rightBarButtonItems addSubview:homeBtn];
[rightBarButtonItems addSubview:settingsBtn];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButtonItems];
Now I want to use below code on same Navigation right bar button:
- (void)viewDidLoad
{
[super viewDidLoad];
// Animation
UIImageView*animationView = [[UIImageView alloc] initWithFrame:self.view.frame];
animationView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.gif"],
[UIImage imageNamed:#"2.gif"],
[UIImage imageNamed:#"3.gif"],
[UIImage imageNamed:#"4.gif"],
[UIImage imageNamed:#"5.gif"],
[UIImage imageNamed:#"6.gif"],nil];
animationView.animationDuration = 1.25;
animationView.animationRepeatCount = 0;
[animationView startAnimating];
[self.view addSubview:animationView];
}
How to work my navigation bar button like the below Images:---
Please send me suitable answer or provide me sample code.
Thank you.
I have tried your code, with a little trick of mine and it work like magic.
NSArray *imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"tmp-0"],
[UIImage imageNamed:#"tmp-1"],
[UIImage imageNamed:#"tmp-2"],
[UIImage imageNamed:#"tmp-3"],
[UIImage imageNamed:#"tmp-4"],
[UIImage imageNamed:#"tmp-5"],
[UIImage imageNamed:#"tmp-6"],
[UIImage imageNamed:#"tmp-7"],nil];
UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barButton setImage:[UIImage imageNamed:#"tmp-0"] forState:UIControlStateNormal]; // mine trick
[barButton.imageView setAnimationImages:imageArray];
[barButton.imageView setAnimationDuration:1.0f];
[barButton.imageView startAnimating];
[barButton sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:barButton];
You need to give a image to barButton at first, so your button can make its frame

UIButton GIF Background Image

How can I use a GIF image as UIButton Image Background?
UIImage *img1 = [UIImage imageNamed:#"btn_img1"];
UIImage *img2 = [UIImage imageNamed:#"btn_img2"];
UIImage *img3 = [UIImage imageNamed:#"btn_img3"];
// GIF ImageView
_btnGIF.imageView.animationImages = [[NSArray alloc] initWithObjects:img1, img2, img3, nil];
_btnGIF.imageView.animationDuration = 0;
[_btnGIF.imageView startAnimating];
//[_btnGIF setBackgroundImage:GIF forState:UIControlStateNormal];
You need to start your button with an image, so it can create it properly.
For example:
UIImage *img1 = [UIImage imageNamed:#"btn_img1"];
UIImage *img2 = [UIImage imageNamed:#"btn_img2"];
UIImage *img3 = [UIImage imageNamed:#"btn_img3"];
[_btnGIF setImage:img1 forState:UIControlStateNormal];
// GIF ImageView
_btnGIF.imageView.animationImages = [[NSArray alloc] initWithObjects:img1, img2, img3, nil];
_btnGIF.imageView.animationDuration = 0;
[_btnGIF.imageView startAnimating];

picture appears as white space in button

I downloaded from a free icon DB a thumb up icon, its size is too large for a button and it had white background.I decided to open it in pixelmator resized the icon and removed the background. when opening it with preview it looks great but when its set to be the button's image in xcode it appears as a white circle.
here is the icon after edit (30x30 png)
and this is how it looks on the button
what should I do to fix this?
edit/ upadte:
I tried #yunas code and it works in the navigation bar not for the toolbar (didn't even show the button).
Trying this code:
UIImage* image = [UIImage imageNamed:#"imageName"];
UIBarButtonItem *likeBtn = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:#selector(action_clicked:)];
NSArray* toolbarItems = [NSArray arrayWithObjects:likeBtn,nil];
self.toolbarItems = toolbarItems;
self.navigationController.toolbarHidden = NO;
resulted again in a button with the white drawing
So I was wondering: Is it even possible to have colored buttons in the toolbar? maybe i'm trying the imposible...
edit/ upadte II - the solution: wrapping a UIButton inside a UIBarButtonItem
#interface StatusUIBarButtonItem : UIBarButtonItem
- (id) initWithStatus: (enum StatusEnum)p_statusEnum;
#end
#implementation StatusUIBarButtonItem
- (instancetype)initWithStatus:(enum StatusEnum)p_statusEnum
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[self getStatusImage] forState:UIControlStateNormal];
[button addTarget:self action:#selector(status_Clicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 20, 20)];
self = [super initWithCustomView:button];
if (self)
{
self.statusButton = button;
}
return self;
}
#end
then in the ViewController class:
-(void) viewDidLoad
{
StatusUIBarButtonItem* stausBtn = [[StatusUIBarButtonItem alloc] initWithStatus:self.status];
NSArray* toolbarItems = [NSArray arrayWithObjects: stausBtn, nil];
self.toolbarItems = toolbarItems;
self.navigationController.toolbarHidden = NO;
}
I hope this will help others that like me want colored images in the toolbar
UIImage *buttonImage = [UIImage imageNamed:#"imageName.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.adjustsImageWhenDisabled = NO;
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = customBarItem;

images from an array

i create an nsmutableArray and i add for it an images. also i create an imageView that i want to show the images from the array, i tried to use animationImages but i nothing is happened, how can i fix it?
here is my code:
//Numbers images
UIImage *numberZero = [UIImage imageNamed:#"numberZero.png"];
UIImage *numberOne = [UIImage imageNamed:#"numberOne.png"];
UIImage *numberTwo = [UIImage imageNamed:#"numberTwo.png"];
UIImage *numberThree = [UIImage imageNamed:#"numberThree.png"];
UIImage *numberFour = [UIImage imageNamed:#"numberFour.png"];
UIImage *numberFive = [UIImage imageNamed:#"numberFive.png"];
UIImage *numberSix = [UIImage imageNamed:#"numberSix.png"];
UIImage *numberSeven = [UIImage imageNamed:#"numberSeven.png"];
UIImage *numberEight = [UIImage imageNamed:#"numberEight.png"];
UIImage *numberNine = [UIImage imageNamed:#"numberNine.png"];
//add numbers uiimage to numbersArray
numbersArray = [NSMutableArray arrayWithObjects:numberZero, numberOne, numberTwo, numberThree, numberFour, numberFive, numberSix, numberSeven, numberEight, numberNine, nil];
imgview1 = [[UIImageView alloc] init];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=0;
[imgview1 startAnimating];
[imgview1 stopAnimating];
thanks!
You need to actually add imgview1 to something so that it displays. If imgview1 was created in Interface Builder then there is no need to assign. Also you stop your animation immediately after starting it.
//If created in IB comment out the next line
imgview1 = [[UIImageView alloc] init];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=0;
//If not created in IB then add this to a view e.g:
[self.view addSubview:imgview1];
[imgview1 startAnimating];
//[imgview1 stopAnimating]; this is too soon to stop animating
I assume that by "nothing happened" you mean that you see the first image, but the sequence does not move beyond that.
This is probably because you stopAnimating too soon: it does not even get a chance to start!
i solve the problem, i guess the problem were because i didn't use init with frame to my imgview.
here is the final code:
UIImageView *imgview1 = [[UIImageView alloc] initWithFrame:CGRectMake(40, 90, 240, 240)];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=1;
[imgview1 startAnimating];
[self.view addSubview:imgview1];
hope u understand...