【问题标题】:How to specify a size to a UIAlert如何为 UIAlert 指定大小
【发布时间】:2011-04-07 09:04:01
【问题描述】:

我正在尝试在 UIAlert 中显示 UIImage。

我想几乎全屏显示它。例如,我的图片大小为 300x450 像素。
好吧,我将它作为子视图添加到我的 UIAlert。

但 UIAlert 具有默认坐标以使其居中。 所以我可以添加比 UIAlert 框架更大的图像,但它会覆盖 UIAlert...

我尝试为我的 UIAlert 指定一个框架,但它对它没有任何影响。

其实我想将我的图片添加为 UIAlert 的真实内容,以及简单的文本。

这可能吗?

【问题讨论】:

标签: iphone objective-c


【解决方案1】:

考虑编写自己的对话框弹出窗口,而不是与 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];
}

【讨论】:

  • 哇,很好的答案。但是我使用了之前共享的链接。无论如何 +1。
【解决方案2】:

是的,这是可能的。

但它需要自定义 UIAlertView。

使用 UIAlertView 自定义,您可以添加任何内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-28
    • 2014-05-24
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    相关资源
    最近更新 更多