Search code examples
iosobjective-cios6uibutton

Add an image to my button in ios is blue


I have a gif in my button, but it displays as just blue box.

 (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
    {
    //[self setTitle:@"Register" forState:UIControlStateNormal];
    [self setBackgroundColor:[UIColor whiteColor]];
    [self setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];



    [self setImage:[UIImage imageNamed:@"Person.gif"] forState:UIControlStateNormal];//setting image on button.
}
return self;

}

The image was added correctly to xcode. What gives?


Solution

  • iOS GIF image not supported so if you wish to do set Image With Animation do like bellow code:-

    UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    animatedImageView.animationImages = [NSArray arrayWithObjects:    
                                   [UIImage imageNamed:@"image1.png"],
                                   [UIImage imageNamed:@"image2.png"],
                                   [UIImage imageNamed:@"image3.png"],
                                   [UIImage imageNamed:@"image4.png"], nil];
    animatedImageView.animationDuration = 1.0f;
    animatedImageView.animationRepeatCount = 0;
    [animatedImageView startAnimating];
    [yourButton addSubview: animatedImageView];
    

    For more Check this Bellow link about iOS suported GIF:-

    Discussions

    Display animated GIF in iOS