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

后端 未结 10 984
孤城傲影
孤城傲影 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 12:10

    There is a user library on cocoacontrols.com that emulates the Android-style Toast pop-ups. Might be what you are looking for.

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

    There's also this one that follows the same idea.

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

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

    Use a UIAlertView with nil title and nil buttons, then dismiss it when desired. Here's how I did this:

    Create an instance variable for the alert view in your .h file:

    @interface StatusMessageController : UIViewController {
        UIAlertView *statusAlert;
    }
    

    In your .m file, create a method to show the alert view and start a timer, and another to handle when the timer expires to dismiss the alert:

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

    Whenever you want to show the status message, invoke it:

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

    At any time, you can also dismiss the alert with:

    [statusAlert dismissWithClickedButtonIndex:0 animated:YES];
    

    You can also change the message on-the-fly with new status:

    statusAlert.message = @"Looking up user";
    
    0 讨论(0)
  • 2020-12-04 12:16

    This is just a Swift 3 version of user2234810 2.2 version.

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

    I created an Android-Kind toast, pretty simple because I don't need more functionality in it for now.

    When it is shown it is added at the bottom of the parent's view, so if that view is the VC's view then it will be at the bottom center of the device.

    The frame is autoadjusted to the text length.

    You use it doing: [self.view addSubview: [[ToastAlert alloc] initWithText: @"Sent"]];, it will be auto-removed so no reference is needed.

    I haven't implemented this, but you can create a static method to shorten and clarify the instruction, sort of: [ToastAlert showText: @"Sent" inView: self.view];.

    The class:

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