Pierre Espenan
Pierre Espenan

Reputation: 4066

How to specify a size to a UIAlert

I am trying to display a UIImage in a UIAlert.

I want to display it nearly fullscreen. For example my image has a 300x450px size.
Well I add it as a subview to my UIAlert.

But the UIAlert has default coordinates to keep it centered. So I can add a image bigger than the UIAlert frame, but it covers the UIAlert...

I tried to specify a frame for my UIAlert, but it has no effect on it.

In fact I would like to add my image as a real content of the UIAlert, as well as simple text.

Is this possible ?

Upvotes: 1

Views: 526

Answers (2)

Wex
Wex

Reputation: 4686

Consider writing your own Dialog popup instead of messing with the UIAlertView. If you want the 'bounce-in' animation that can easily be achieved using transform animations. Here is a simple popup dialog box I've just made.

To test it out create a new View-Based Application project and add this class. Then you can test it by adding the 'usage' code below to your *ViewController.m

This is a very simple example to demo the theory. It would make more sense to have a static method in SimpleDialog that shows the popup, but I didn't want to make the example overly complex.


SimpleDialog.h

#import <UIKit/UIKit.h>

@interface SimpleDialog : UIView
{
    
}

- (void) show;

@end

SimpleDialog.m

#import "SimpleDialog.h"

#define BOUNCE_SPEED 1

@implementation SimpleDialog

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        self.backgroundColor = [UIColor greenColor];
        
        UIButton* closeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        closeButton.frame = CGRectMake(0, 0, 200, 20);
        closeButton.center = self.center;
        [closeButton setTitle:@"Close" forState:UIControlStateNormal];
        [closeButton addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:closeButton];
    }
    return self;
}

- (void) show
{
    UIWindow* window = [UIApplication sharedApplication].keyWindow;
    if (!window)
    {
        window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    }
    [window addSubview:self];
    
    self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.2*BOUNCE_SPEED];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(bounceInStopped)];
    self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
    [UIView commitAnimations];
}

- (void)bounceInStopped
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.15*BOUNCE_SPEED];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(bounceOutStopped)];
    self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
    [UIView commitAnimations];
}

- (void)bounceOutStopped
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.15*BOUNCE_SPEED];
    self.transform = CGAffineTransformIdentity;
    [UIView commitAnimations];
}

- (void) hide
{
    [self removeFromSuperview];
}

@end

Usage

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIButton* popButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    popButton.frame = CGRectMake(0, 0, 200, 20);
    popButton.center = self.view.center;
    [popButton setTitle:@"Pop" forState:UIControlStateNormal];
    [popButton addTarget:self action:@selector(showIt) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:popButton];
}

- (void) showIt
{
    SimpleDialog* simple = [[SimpleDialog alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
    simple.center = self.view.center;
    [self.view addSubview:simple];
    [simple show];
    [simple release];
}

Upvotes: 2

Chetan Bhalara
Chetan Bhalara

Reputation: 10344

Yes this is possible.

But it requires to customize the UIAlertView.

Using UIAlertView customization you can add anything.

Upvotes: 0

Related Questions