2011-04-07 42 views
1

我想在UIAlert中显示一个UIImage。如何指定一个UIAlert的大小

我想近乎全屏地显示它。例如,我的图片的大小为300x450px。
那么我把它作为子视图添加到我的UIAlert中。

但UIAlert具有默认坐标以保持居中。 因此,我可以添加一个比UIAlert帧大的图像,但它覆盖了UIAlert ...

我试图为我的UIAlert指定一个帧,但它对它没有影响。

事实上,我想添加我的图像作为UIAlert的真实内容,以及简单的文本。

这可能吗?

+0

检查此链接http://stackoverflow.com/q/3975347/644149 – 2011-04-07 09:13:27

回答

0

是的,这是可能的。

但它需要定制UIAlertView。

使用UIAlertView自定义您可以添加任何东西。

2

考虑编写自己的对话框弹出窗口而不是搞乱UIAlertView。如果您想要使用变形动画轻松实现的“跳出”动画。这是我刚刚制作的一个简单的弹出对话框。

要测试它,请创建一个新的基于视图的应用程序项目并添加此类。然后,您可以通过将以下'使用'代码添加到您的* ViewController.m

这是一个演示该理论的非常简单的示例。在SimpleDialog中有一个显示弹出窗口的静态方法会更有意义,但我不想让这个例子过于复杂。


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 

用法
- (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]; 
} 
+0

哇,很好的答案。但我使用了前一个共享的链接。无论如何。 – 2011-04-07 12:14:12

相关问题