How to display temporary popup message on iPhone/iPad/iOS

后端 未结 10 983
孤城傲影
孤城傲影 2020-12-04 11:31

I\'d like to display a temporary message on the iPhone/iPad displaying confirmation of an action, or some quick status about some background activity.

Is there a st

相关标签:
10条回答
  • 2020-12-04 11:54

    Use UIAlertView. Here's a quick link to a simple example:

    UIAlertView

    Edited: Sorry, didn't see about disappearing on it's own. I think you need to have some confirmation by users of messages received. UIAlertView pretty much accomplishes that. Not sure if you have it gradually fade away unless you're in an app with a view that has a timer or event based delay that will show a view and then eventually remove it.

    Second edit: Found a way to implement it. You can manually call the dismiss function after some set time that you've designated. (Sorry for the messy post) It would look something like this:

    //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 讨论(0)
  • 2020-12-04 11:58

    Create a class that inherits from UIAlertView. In your constructor just call [super init] and then add any view you want as a subview. You can even design this view in Interface Builder. Something like this:

    - (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]; 
    }
    

    To display your custom alert view just init it, and call show as with a regular UIAlertView.

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

    As an nice side-effect, when you present this view the background will darken and you will get the nice wobbly animation for any alert view.

    0 讨论(0)
  • 2020-12-04 11:58

    *Swift 2.2 Answer:

    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)
    
    0 讨论(0)
  • 2020-12-04 11:58

    You can use my own StatusAlert framework written in Swift. It gives you ability to show Apple system-like alert as well as present the same alert without an image, title or message anywhere in UIView.

    It is available via Cocoapods and Carthage and supports iPhone X, Safe Areas layout, iPads and allows some customizations.

    0 讨论(0)
  • 2020-12-04 12:03

    Similar answer to @marco-mustapic's, but without inheritance.

    - (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
        ];
    }
    

    To use it:

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

    Throw it into a category on NSObject or something to always have it around.

    0 讨论(0)
  • 2020-12-04 12:04

    I ended up creating my own class. Didn't inherit from UIAlertView. General structure is,

    -(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 讨论(0)
提交回复
热议问题