【问题标题】:How to display temporary popup message on iPhone/iPad/iOS如何在 iPhone/iPad/iOS 上显示临时弹出消息
【发布时间】:2011-04-13 20:49:14
【问题描述】:

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

是否有执行此操作的标准控件?我见过应用程序这样做。一个圆角矩形,深色且部分透明,里面有文字。它不要求用户输入,但会在短时间内自行消失。 Android 有一个类似的标准结构。也类似于 Growl 显示的窗口。

建议表示赞赏。

【问题讨论】:

    标签: iphone ipad popup alert


    【解决方案1】:

    cocoacontrols.com 上有一个模拟 Android 风格的 Toast 弹出窗口的用户库。可能是您正在寻找的。​​p>

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

    还有一个遵循相同想法的。

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

    【讨论】:

    【解决方案2】:

    创建一个继承自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]; 
    }
    

    要显示您的自定义警报视图,只需初始化它,然后像普通的 UIAlertView 一样调用 show

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

    作为一个不错的副作用,当您呈现此视图时,背景会变暗,并且您将获得任何警报视图的漂亮摇摆动画。

    【讨论】:

    • UIAlertView 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。
    【解决方案3】:

    使用 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];
    

    【讨论】:

    • 我使用没有按钮的 UIAlertView 实现了一些快速而肮脏的东西。延迟后我自己将其关闭。它有效,但看起来没有我描述的那么好。如果它没有内置到平台中,则必须在某处实现。自己实现似乎并不难,但似乎有人应该已经做到了。应用程序“FeeddlerRSS”经常使用类似的东西。
    • 我找到了另一个类似问题的帖子。我认为这更像是您在说的:stackoverflow.com/questions/593147/…
    【解决方案4】:

    使用带有 nil 标题和 nil 按钮的 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 dismissWithClickedButtonIndex:0 animated:YES];
    

    您还可以使用新状态即时更改消息:

    statusAlert.message = @"Looking up user";
    

    【讨论】:

      【解决方案5】:

      我创建了一个 Android 类型的 toast,非常简单,因为我现在不需要更多功能。

      当它显示时,它被添加到父视图的底部,因此如果该视图是 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
      

      【讨论】:

        【解决方案6】:

        我最终创建了自己的课程。没有从 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]; }];
        }
        

        【讨论】:

        • 请注意,您必须先 '#import ' 才能访问视图层的cornerRadius 属性。
        • 嘿,我用过这段代码。它工作正常。但它只加载一次。当文本字段再次聚焦时它不起作用。有什么解决办法吗?
        【解决方案7】:

        与@marco-mustapic 的答案类似,但没有继承。

        - (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 上的一个类别或其他东西中以始终拥有它。

        【讨论】:

          【解决方案8】:

          *Swift 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)
          

          【讨论】:

            【解决方案9】:

            这只是 user2234810 2.2 版本的 Swift 3 版本

            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)
            

            【讨论】:

              【解决方案10】:

              您可以使用我自己用 Swift 编写的 StatusAlert 框架。它使您能够在UIView 的任何位置显示类似 Apple 系统的警报,以及在没有图像、标题或消息的情况下显示相同的警报。

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

              【讨论】:

              • 干得好。有一个禁用模糊的属性会很好。如果isBlurAvailable 为真,则忽略后台设置。
              猜你喜欢
              • 2021-06-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-06-25
              • 2012-05-28
              • 1970-01-01
              • 2020-02-29
              • 1970-01-01
              相关资源
              最近更新 更多