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

相關問題