I have made Android application a few months ago. The Toast class is very useful for me. I do not need to consider the main Thread and place to show it. Anywhere I can show it and just leave that and it is automatically disappeared.
Toast.makeToast(context, msg, Toast.LENGTH_SHORT).show();
That's it. ^^
What about iPhone? Is there something like the Toast? Just show message and do not need to care about it. It will be automatically disappeared.
Toasts are a native feature of Android, but iOS doesn't have this by default. If you only need toasts on Android, you can use the ToastAndroid API provided by React Native.
A toast message in iOS is a small, short-lived popup that provides a small bite of information to the users. In this iOS Swift tutorial, we will learn how to implement an iOS toast message in your iPhone app, but we will also show how to add animation to that.
Toast in iOS In Android, Toast will automatically show a quick message on the device, and fade away after a short while. It's a very useful debugging feature without need to look at Android Studio or the console. In iOS, however, there's no such equivalent API.
android.widget.Toast. A toast is a view containing a quick little message for the user. The toast class helps you create and show those. When the view is shown to the user, appears as a floating view over the application. It will never receive focus.
I've been writing for Android for a long time and I am missing Toast. I've implemented one. Need code? here you are:
ToastView.h
#import <UIKit/UIKit.h> @interface ToastView : UIView @property (strong, nonatomic) NSString *text; + (void)showToastInParentView: (UIView *)parentView withText:(NSString *)text withDuaration:(float)duration; @end
ToastView.m
#import "ToastView.h" @interface ToastView () @property (strong, nonatomic, readonly) UILabel *textLabel; @end @implementation ToastView @synthesize textLabel = _textLabel; float const ToastHeight = 50.0f; float const ToastGap = 10.0f; - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } -(UILabel *)textLabel { if (!_textLabel) { _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 5.0, self.frame.size.width - 10.0, self.frame.size.height - 10.0)]; _textLabel.backgroundColor = [UIColor clearColor]; _textLabel.textAlignment = NSTextAlignmentCenter; _textLabel.textColor = [UIColor whiteColor]; _textLabel.numberOfLines = 2; _textLabel.font = [UIFont systemFontOfSize:13.0]; _textLabel.lineBreakMode = NSLineBreakByCharWrapping; [self addSubview:_textLabel]; } return _textLabel; } - (void)setText:(NSString *)text { _text = text; self.textLabel.text = text; } + (void)showToastInParentView: (UIView *)parentView withText:(NSString *)text withDuaration:(float)duration; { //Count toast views are already showing on parent. Made to show several toasts one above another int toastsAlreadyInParent = 0; for (UIView *subView in [parentView subviews]) { if ([subView isKindOfClass:[ToastView class]]) { toastsAlreadyInParent++; } } CGRect parentFrame = parentView.frame; float yOrigin = parentFrame.size.height - (70.0 + ToastHeight * toastsAlreadyInParent + ToastGap * toastsAlreadyInParent); CGRect selfFrame = CGRectMake(parentFrame.origin.x + 20.0, yOrigin, parentFrame.size.width - 40.0, ToastHeight); ToastView *toast = [[ToastView alloc] initWithFrame:selfFrame]; toast.backgroundColor = [UIColor darkGrayColor]; toast.alpha = 0.0f; toast.layer.cornerRadius = 4.0; toast.text = text; [parentView addSubview:toast]; [UIView animateWithDuration:0.4 animations:^{ toast.alpha = 0.9f; toast.textLabel.alpha = 0.9f; }completion:^(BOOL finished) { if(finished){ } }]; [toast performSelector:@selector(hideSelf) withObject:nil afterDelay:duration]; } - (void)hideSelf { [UIView animateWithDuration:0.4 animations:^{ self.alpha = 0.0; self.textLabel.alpha = 0.0; }completion:^(BOOL finished) { if(finished){ [self removeFromSuperview]; } }]; } @end
Call from ViewController
[ToastView showToastInParentView:self.view withText:@"What a toast!" withDuaration:5.0];
There is no class "out-of-the-box" in UIKit to do this. But it is quite easy to create a class that will offer this behavior.
You just have to create a class that inherit from UIView. This class will have the responsibility - to create what you want to display, - to add itself in parent view hierarchy - to dismiss itself using a timer.
You will be able to use it like :
[ToastView toastViewInView:myParentView withText:@"what a wonderful text"];
Regards, Quentin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With