2010-09-17 174 views
30

我想在iPhone/iPad上显示一条临时消息,显示操作确认或有关某些后台活动的快速状态。如何在iPhone/iPad/iOS上显示临时弹出消息

有没有一个标准的控制来做到这一点?我见过应用程序这样做。一个圆角矩形,黑色,部分透明,里面有文字。它不要求用户输入,而是在短时间内自行消失。 Android有一个类似的标准构造。也类似于咆哮显示的窗口。

建议感激。

回答

15

cocoacontrols.com上有一个模拟Android风格Toast弹出窗口的用户库。可能是你在找什么。

http://www.cocoacontrols.com/platforms/ios/controls/altoastview

还有这一个遵循同样的想法。

http://www.cocoacontrols.com/platforms/ios/controls/itoast

+1

这与我所寻找的最接近,但我最终写了自己的。 – David 2011-11-26 01:42:32

+0

我发现ALToastView有用,谢谢! – 2012-01-12 05:47:03

+1

更新:不断流行的[MBProgressHUD](https://github.com/jdg/MBProgressHUD)现在有[Toast-like通知](http://dl.dropbox.com/u/378729/MBProgressHUD/7。 PNG)。 – 2012-08-07 21:59:38

9

使用UIAlertView。这里有一个快速链接到一个简单的例子:

UIAlertView

编辑:对不起,没看到消失在它自己的。我认为你需要得到用户收到的消息的确认。 UIAlertView几乎可以做到这一点。不知道你是否已经逐渐消失,除非你在一个具有计时器或基于事件延迟的视图的应用程序中显示一个视图,然后最终删除它。

第二次编辑:找到了实现它的方法。您可以在指定的一段设定时间后手动调用解除功能。 (对不起,凌乱后)这将是这个样子:

//Create UIAlertView alert 
alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Some message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles: nil]; 

//After some time 
[alert dismissWithClickedButtonIndex:0 animated:TRUE]; 
+0

我使用不带按钮的UIAlertView中实现的东西快速和肮脏。我拖延后自己解雇了。它有效,但看起来没有我所描述的那么好。如果它没有内置到平台中,那么必须有某个实现。似乎不是很难自己实现,但似乎有人应该已经做到了。应用程序“FeeddlerRSS”不断地使用它。 – David 2010-09-17 18:38:38

+0

我发现了另一个类似问题的帖子。我认为这更多的是你在说什么:http://stackoverflow.com/questions/593147/how-to-display-a-progress-indicator-overlay-hud-on-iphone – Kennzo 2010-09-17 18:56:28

15

创建从UIAlertView继承的类。在你的构造函数中调用[super init]然后添加你想要的任何视图作为子视图。你甚至可以在Interface Builder中设计这个视图。事情是这样的:

- (id)initWithMessage:(NSString *)message dismissAfter:(NSTimeInterval)interval 
{ 
    if ((self = [super init])) 
    { 
    CustomView * customView = [[[CustomView alloc] init] autorelease]; // or load from NIB 
    [self addSubview:customView]; 
    [self performSelector:@selector(dismissAfterDelay) withObject:nil afterDelay:interval]; 
    } 
    return self; 
} 

- (void)dismissAfterDelay 
{ 
    [self dismissWithClickedButtonIndex:0 animated:YES]; 
} 

要显示自定义警报视图只是初始化它,并调用show与常规UIAlertView

CustomAlertView * cav = [[CustomAlertView alloc] initWithMessage:@"Doing Something]; 
[cav show]; 
[cav release]; 

作为一个很好的副作用,当你提出这个观点的背景会变暗,你会得到漂亮的不稳定动画任何警报视图。

+2

UIAlertView类旨在按原样使用,不支持子类化。该类的视图层次结构是私有的,不能修改。 – 2013-05-29 18:14:32

4

我结束了创建我自己的类。没有从UIAlertView继承。一般结构是,

-(id)initWithText:(NSString *)msg { 
    // Create a view. Put a label, set the msg 
    CALayer *layer = self.layer; 
    layer.cornerRadius = 8.0f; 
    ... 
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.8]; 
    [self performSelector:@selector(dismiss:) withObject:nil afterDelay:2.0]; 
    [self setAutoresizesSubviews:FALSE]; 
    return self; 
} 


- (void)dismiss:(id)sender { 
    // Fade out the message and destroy self 
    [UIView animateWithDuration:0.5 
       animations:^ { self.alpha = 0; } 
       completion:^ (BOOL finished) { [self removeFromSuperview]; }]; 
} 
+0

请注意,您必须先'#import '才能访问视图图层的cornerRadius属性。 – 2011-11-28 17:40:58

+0

嘿,我已经使用这段代码。 它的工作正常。但它只加载一次。当文本字段重新聚焦时它不起作用。任何解决方案。? – 2012-05-16 09:26:11

8

我创建一个Android类面包,很简单,因为我不现在它需要更多的功能。

当显示它被添加到父视图的底部时,所以如果该视图是VC的视图,那么它将位于设备的底部中心。

该帧被自动调整为文本长度。

您使用它:[self.view addSubview: [[ToastAlert alloc] initWithText: @"Sent"]];,它将被自动删除,因此不需要参考。

我还没有实现这个,但你可以创建一个静态方法来缩短和澄清指令,排序:[ToastAlert showText: @"Sent" inView: self.view];

类:

ToastAlert.h

@interface ToastAlert : UILabel { 

} 

- (id)initWithText: (NSString*) msg; 

@end 

ToastAlert.m

#import "ToastAlert.h" 
#import <QuartzCore/QuartzCore.h> 

@implementation ToastAlert 

#define POPUP_DELAY 1.5 

- (id)initWithText: (NSString*) msg 
{ 

    self = [super init]; 
    if (self) { 

     self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7]; 
     self.textColor = [UIColor colorWithWhite:1 alpha: 0.95]; 
     self.font = [UIFont fontWithName: @"Helvetica-Bold" size: 13]; 
     self.text = msg; 
     self.numberOfLines = 0; 
     self.textAlignment = UITextAlignmentCenter; 
     self.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 


    } 
    return self; 
} 

- (void)didMoveToSuperview { 

    UIView* parent = self.superview; 

    if(parent) { 

     CGSize maximumLabelSize = CGSizeMake(300, 200); 
     CGSize expectedLabelSize = [self.text sizeWithFont: self.font constrainedToSize:maximumLabelSize lineBreakMode: NSLineBreakByTruncatingTail]; 

     expectedLabelSize = CGSizeMake(expectedLabelSize.width + 20, expectedLabelSize.height + 10); 

     self.frame = CGRectMake(parent.center.x - expectedLabelSize.width/2, 
           parent.bounds.size.height-expectedLabelSize.height - 10, 
           expectedLabelSize.width, 
           expectedLabelSize.height); 

     CALayer *layer = self.layer; 
     layer.cornerRadius = 4.0f; 

     [self performSelector:@selector(dismiss:) withObject:nil afterDelay:POPUP_DELAY]; 
    } 
} 

- (void)dismiss:(id)sender { 
    // Fade out the message and destroy self 
    [UIView animateWithDuration:0.6 delay:0 options: UIViewAnimationOptionAllowUserInteraction 
        animations:^ { self.alpha = 0; } 
        completion:^ (BOOL finished) { [self removeFromSuperview]; }]; 
} 

@end 
8

使用带有零标题和零按钮的UIAlertView中,然后在需要时清除它。以下是我这样做:

在.h文件中创建警报视图的实例变量:

@interface StatusMessageController : UIViewController { 
    UIAlertView *statusAlert; 
} 

在您.m文件,创建一个方法来显示警报视图,并启动一个定时器,当计时器到期解除警报另一个处理:每当你想显示状态信息

- (void)showStatus:(NSString *)message timeout:(double)timeout { 
    statusAlert = [[UIAlertView alloc] initWithTitle:nil 
                message:message 
                delegate:nil 
              cancelButtonTitle:nil 
              otherButtonTitles:nil]; 
    [statusAlert show]; 
    [NSTimer scheduledTimerWithTimeInterval:timeout 
            target:self 
            selector:@selector(timerExpired:) 
            userInfo:nil 
            repeats:NO]; 
} 

- (void)timerExpired:(NSTimer *)timer { 
    [statusAlert dismissWithClickedButtonIndex:0 animated:YES]; 
} 

,调用它:

[self showStatus:@"Computing" timeout:4.5]; 

在任何时候,你也可以关闭与警报:

statusAlert.message = @"Looking up user"; 
+0

完美地工作,谢谢! – marimaf 2014-07-11 23:50:20

2

类似的答案@马尔科 - mustapic的:

[statusAlert dismissWithClickedButtonIndex:0 animated:YES]; 

您也可以更改消息与新的状态上的飞,但没有继承。

- (void)dismissAlert:(UIAlertView *)alertView 
{ 
    [alertView dismissWithClickedButtonIndex:0 animated:YES]; 
} 

- (void)showPopupWithTitle:(NSString *)title 
        mesage:(NSString *)message 
       dismissAfter:(NSTimeInterval)interval 
{ 
    UIAlertView *alertView = [[UIAlertView alloc] 
      initWithTitle:title 
       message:message 
       delegate:nil 
     cancelButtonTitle:nil 
     otherButtonTitles:nil 
    ]; 
    [alertView show]; 
    [self performSelector:@selector(dismissAlert:) 
       withObject:alertView 
       afterDelay:interval 
    ]; 
} 

要使用它:

[self showPopupWithTitle:@"Hi" message:@"I like pie" dismissAfter:2.0]; 

扔到上NSObject的类别或东西总是有它周围。

+0

我喜欢这个。正好。 – 2015-12-03 18:42:22

2

*雨燕2.2答:

func showPopupWithTitle(title: String, message: String, interval: NSTimeInterval) { 
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert) 
    presentViewController(alertController, animated: true, completion: nil) 
    self.performSelector(#selector(dismissAlertViewController), withObject: alertController, afterDelay: interval) 
} 

func dismissAlertViewController(alertController: UIAlertController) { 
    alertController.dismissViewControllerAnimated(true, completion: nil) 
} 

showPopupWithTitle("Title", message: "Message", interval: 0.5) 
1

这仅仅是一个斯威夫特3user2234810 2.2版本的版本。

func showPopupWithTitle(title: String, message: String, interval: TimeInterval) { 
    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) 
    present(alertController, animated: true, completion: nil) 
    self.perform(#selector(dismissAlertViewController), with: alertController, afterDelay: interval) 
} 

func dismissAlertViewController(alertController: UIAlertController) { 
    alertController.dismiss(animated: true, completion: nil) 
} 
showPopupWithTitle(title: "Title", message: "Message", interval: 0.5) 
0

你可以用我自己的StatusAlert框架写成Swift。它使您能够显示类似Apple系统的警报,并在UIView的任何地方提供没有图像,标题或消息的相同警报。

它可通过Cocoapods和Carthage获得并支持iPhone X,安全区域布局,iPad和允许一些自定义设置。

StatusAlertExample application screenshow