Android Toast equivalent in iOS Android Toast equivalent in iOS ios ios

Android Toast equivalent in iOS


I found this amazing class in github that works like a charm.Toast for iOSIt is enough to import the UIView+Toast.h and UIView+Toast.m files and then add

[self.view makeToast:@"This is a piece of toast."];

as written in the page examples.


I handled it with a simple static UI Helper method using the Key Window:

+(void)displayToastWithMessage:(NSString *)toastMessage{    [[NSOperationQueue mainQueue] addOperationWithBlock:^ {        UIWindow * keyWindow = [[UIApplication sharedApplication] keyWindow];        UILabel *toastView = [[UILabel alloc] init];        toastView.text = toastMessage;        toastView.font = [MYUIStyles getToastHeaderFont];        toastView.textColor = [MYUIStyles getToastTextColor];        toastView.backgroundColor = [[MYUIStyles getToastBackgroundColor] colorWithAlphaComponent:0.9];        toastView.textAlignment = NSTextAlignmentCenter;        toastView.frame = CGRectMake(0.0, 0.0, keyWindow.frame.size.width/2.0, 100.0);        toastView.layer.cornerRadius = 10;        toastView.layer.masksToBounds = YES;        toastView.center = keyWindow.center;        [keyWindow addSubview:toastView];        [UIView animateWithDuration: 3.0f                          delay: 0.0                        options: UIViewAnimationOptionCurveEaseOut                     animations: ^{                         toastView.alpha = 0.0;                     }                     completion: ^(BOOL finished) {                         [toastView removeFromSuperview];                     }         ];    }];}


There is no android toast equivalent in iOS.

But there are always workarounds like

you can animate a view and play with its alpha

The below is just sample code not a solution

UIView beginAnimations:nil context:NULL];[UIView setAnimationDuration:3.0f];imageView.alpha = 0.0f;[UIView commitAnimations];

if you dont want to slowly fade within 3 seconds, you can use

[UIView setAnimationDelay:3];

and reduce the animation duraction to 0.5f or something. i think using a short fade out time feels better than just simply set hide to YES


matomo