Android Toast equivalent in iOS
Solution 1
I found this amazing class in github that works like a charm. Toast for iOS It 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.
Solution 2
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];
}
];
}];
}
Solution 3
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
Solution 4
We've implemented something like this in our open source library Raisin Toast. Setup is quite straightforward once you add the files to your project:
Add a property to your app delegate:
@property (strong, nonatomic) RZMessagingWindow *errorWindow;
Create the default messaging window:
self.errorWindow = [RZMessagingWindow defaultMessagingWindow];
[RZErrorMessenger setDefaultMessagingWindow:self.errorWindow];
and then one line to present the messaging window:
[RZErrorMessenger displayErrorWithTitle:@"Whoops!" detail:@"Something went horribly wrong and we accidentally cut off the wrong leg"];
The demo project highlights some of the more advanced customizations of adding images and custom styles.
The implementation uses a second UIWindow and because of the RZErrorMessenger class method it's available everywhere.
Solution 5
Maybe this can help someone:
NSString *message = @"Toast kind of message";
UIAlertView *toast = [[UIAlertView alloc] initWithTitle:nil
message:message
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil, nil];
[toast show];
int duration = 1; // in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[toast dismissWithClickedButtonIndex:0 animated:YES];
});
UPDATE UIAlertView is deprecated in IOS 8. Here the new way:
NSString *message = @"Toast kind of message";
UIAlertController *toast =[UIAlertController alertControllerWithTitle:nil
message:message
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:toast animated:YES completion:nil];
int duration = 1; // in seconds
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[toast dismissViewControllerAnimated:YES completion:nil];
});
EDIT: For the ones that using Xamarin.IOS you can do like this:
new UIAlertView(null, message, null, "OK", null).Show();
using UIKit; is required.
Admin
Updated on September 21, 2020Comments
-
Admin over 3 years
Does anyone know what the Java Toast equivalent of this iOS Objective C event would be in a Fragment? Below is a sample of what I have written in iOS. What I am looking for the same Alert in Java using a Toast in place of the iOS UIAlert. I am sorry if I did not make that clear on my original post.
- (void) dateLogic { NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"MMMM dd"]; NSString *theDate = [dateFormat stringFromDate:[NSDate date]]; //JANUARY if ([theDate isEqualToString:@"January 01"]) { feastDay = [[UIAlertView alloc] initWithTitle:@"New Years Day!" message:@"January 01" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Close", nil]; feastDay.delegate = self; [feastDay show]; } }