Displaying a message in iOS which has the same functionality as Toast in Android

前端 未结 19 1700
逝去的感伤
逝去的感伤 2020-12-12 12:35

I need to know if there is any method in iOS which behaves like Toast messages in Android. That is, I need to display a message which is dismissed automatically after few se

相关标签:
19条回答
  • 2020-12-12 13:30

    For the ones that using Xamarin.IOS you can do like this:

    new UIAlertView(null, message, null, "OK", null).Show();
    

    using UIKit; is required.

    0 讨论(0)
  • 2020-12-12 13:32

    Again if using IOS on Xamarin there is a new component called BTProgressHUD in the component store

    0 讨论(0)
  • 2020-12-12 13:33

    For Swift 3 & 4:

    Use Toaster library

    Toast(text: "Hello, world!", duration: Delay.long)
    

    For Swift 2:

    Use JLToast

    0 讨论(0)
  • 2020-12-12 13:34

    For me this solution works fine: https://github.com/cruffenach/CRToast

    Example how use it:

        NSDictionary *options = @{
                              kCRToastTextKey : @"Hello World!",
                              kCRToastTextAlignmentKey : @(NSTextAlignmentCenter),
                              kCRToastBackgroundColorKey : [UIColor redColor],
                              kCRToastAnimationInTypeKey : @(CRToastAnimationTypeGravity),
                              kCRToastAnimationOutTypeKey : @(CRToastAnimationTypeGravity),
                              kCRToastAnimationInDirectionKey : @(CRToastAnimationDirectionLeft),
                              kCRToastAnimationOutDirectionKey : @(CRToastAnimationDirectionRight)
                              };
    [CRToastManager showNotificationWithOptions:options
                                completionBlock:^{
                                    NSLog(@"Completed");
                                }];
    
    0 讨论(0)
  • 2020-12-12 13:35

    You can make use of MBProgressHUD project.

    Use HUD mode MBProgressHUDModeText for toast-like behaviour,

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    
    // Configure for text only and offset down
    hud.mode = MBProgressHUDModeText;
    hud.label.text = @"Some message...";
    hud.margin = 10.f;
    hud.yOffset = 150.f;
    hud.removeFromSuperViewOnHide = YES;
    
    [hud hideAnimated:YES afterDelay:3];
    

    enter image description here

    0 讨论(0)
  • 2020-12-12 13:35

    Swift Implementation of Android Toast using Alert which dissipate after 3 secs.

        func showAlertView(title: String?, message: String?) {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
        let okAction = UIAlertAction(title: "OK", style: .Cancel, handler: nil)
        alertController.addAction(okAction)
        self.presentViewController(alertController, animated: true, completion: nil)
    
    
        let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(3 * Double(NSEC_PER_SEC)))
        dispatch_after(delayTime, dispatch_get_main_queue()) {
            print("Bye. Lovvy")
            alertController.dismissViewControllerAnimated(true, completion: nil)
        }
    }
    

    To Call it simply :

    self.showAlertView("Message sent...", message: nil)
    
    0 讨论(0)
提交回复
热议问题