Android中的Toast相当于iOS

有没有人知道这个iOS Objective C事件的Java Toast等价物是在片段中? 以下是我在iOS中编写的示例。 我正在寻找在Java中使用Toast代替iOS UIAlert的警报。 如果我原来的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]; } } 

Solutions Collecting From Web of "Android中的Toast相当于iOS"

我在github上发现了这个惊人的类,就像一个魅力。 Toast for iOS只需导入UIView + Toast.h和UIView + Toast.m文件,然后添加

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

就像写在页面的例子。

在iOS中没有相当于android的烤面包。

但总是有一些解决方法

您可以为视图添加animation,并使用alpha来播放video

下面只是示例代码而不是解决scheme

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

如果你不想在3秒内慢慢淡出,可以使用

 [UIView setAnimationDelay:3]; 

并将animation拖放到0.5f或者其他东西。 我认为使用一个简短的淡出时间比只是简单地将隐藏设置为YES更好

我们在开源库Raisin Toast中实现了这样的function。 一旦将文件添加到项目中,安装程序非常简单:

将属性添加到您的应用程序委托:

 @property (strong, nonatomic) RZMessagingWindow *errorWindow; 

创build默认消息窗口:

 self.errorWindow = [RZMessagingWindow defaultMessagingWindow]; [RZErrorMessenger setDefaultMessagingWindow:self.errorWindow]; 

然后一行显示消息窗口:

 [RZErrorMessenger displayErrorWithTitle:@"Whoops!" detail:@"Something went horribly wrong and we accidentally cut off the wrong leg"]; 

演示项目突出显示了一些更高级的添加图像和自定义样式的自定义设置。

该实现使用第二个UIWindow,并且由于RZErrorMessenger类方法,它在任何地方都是可用的。

我使用Key Window使用简单的静态UI Helper方法处理它:

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

也许这可以帮助某人:

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

编辑:对于那些使用Xamarin.IOS你可以这样做:

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

使用UIKit; 是必须的。

Swift 2.0:

使用https://github.com/Rannie/Toast-Swift/blob/master/SwiftToastDemo/Toast/HRToast%2BUIView.swift

下载HRToast + UIView.swift类并拖放到项目中。 确保在对话框中选中“需要时复制项目”。

  //Usage: self.view.makeToast(message: "Simple Toast") self.view.makeToast(message: "Simple Toast", duration: 2.0, position:HRToastPositionTop) self.view.makeToast(message: "Simple Toast", duration: 2.0, position: HRToastPositionCenter, image: UIImage(named: "ic_120x120")!) self.view.makeToast(message: "It is just awesome", duration: 2.0, position: HRToastPositionDefault, title: "Simple Toast") self.view.makeToast(message: "It is just awesome", duration: 2.0, position: HRToastPositionCenter, title: "Simple Toast", image: UIImage(named: "ic_120x120")!) self.view.makeToastActivity() self.view.makeToastActivity(position: HRToastPositionCenter) self.view.makeToastActivity(position: HRToastPositionDefault, message: "Loading") self.view.makeToastActivityWithMessage(message: "Loading") 

如果你真的想只是Android的吐司外观,然后尝试这个库,它运作良好,已经在我的几个应用程序内使用

https://github.com/ecstasy2/toast-notifications-ios运作良好…

我决定拆开一个更完整的Xamarin.iOS / MonoTouch解决scheme,对我来说非常好。

  private async Task ShowToast(string message, UIAlertView toast = null) { if (null == toast) { toast = new UIAlertView(null, message, null, null, null); toast.Show(); await Task.Delay(2000); await ShowToast(message, toast); return; } UIView.BeginAnimations(""); toast.Alpha = 0; UIView.CommitAnimations(); toast.DismissWithClickedButtonIndex(0, true); } 

如果方法是从后台线程调用的(不是主UI线程),则需要BeginInvokeOnMainThread,这意味着只需调用它就可以了。

 BeginInvokeOnMainThread(() => { ShowToast(message); }); 

目标C

 +(void)showPositiveMessage :(NSString*)message{ [ViewController showAlertWithBackgroundColor:[UIColor greenColor] textColor:[UIColor whiteColor] message:message];} +(void)showNegativeMessage :(NSString*)message{ [ViewController showAlertWithBackgroundColor:[UIColor redColor] textColor:[UIColor whiteColor] message:message];} +(void)showAlertWithBackgroundColor:(UIColor*)backgroundColor textColor:(UIColor*)textColor message:(NSString*)String{ AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; UILabel* label = [[UILabel alloc] initWithFrame:CGRectZero]; label.textAlignment = NSTextAlignmentCenter; label.text = String; label.font = [UIFont fontWithName:@"HelveticaNeue" size:FONTSIZE]; label.adjustsFontSizeToFitWidth = true; [label sizeToFit]; label.numberOfLines = 4; label.layer.shadowColor = [UIColor grayColor].CGColor; label.layer.shadowOffset = CGSizeMake(4, 3); label.layer.shadowOpacity = 0.3; label.frame = CGRectMake(320, 64, appDelegate.window.frame.size.width, 44); label.alpha = 1; label.backgroundColor = backgroundColor; label.textColor = textColor; [appDelegate.window addSubview:label]; CGRect basketTopFrame = label.frame; basketTopFrame.origin.x = 0; [UIView animateWithDuration:2.0 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity:0.1 options:UIViewAnimationOptionCurveEaseOut animations: ^(void){ label.frame = basketTopFrame; } completion:^(BOOL finished){ [label removeFromSuperview]; } ];} 

迅速

如何在Swift中敬酒信息?

一个Swift 3解决scheme准备复制粘贴:

 import UIKit public extension UIView { public func showToast(message:String, duration:Int = 2000) { let toastLabel = UIPaddingLabel(); toastLabel.padding = 10; toastLabel.translatesAutoresizingMaskIntoConstraints = false; toastLabel.backgroundColor = UIColor.darkGray; toastLabel.textColor = UIColor.white; toastLabel.textAlignment = .center; toastLabel.text = message; toastLabel.numberOfLines = 0; toastLabel.alpha = 0.9; toastLabel.layer.cornerRadius = 20; toastLabel.clipsToBounds = true; self.addSubview(toastLabel); self.addConstraint(NSLayoutConstraint(item:toastLabel, attribute:.left, relatedBy:.greaterThanOrEqual, toItem:self, attribute:.left, multiplier:1, constant:20)); self.addConstraint(NSLayoutConstraint(item:toastLabel, attribute:.right, relatedBy:.lessThanOrEqual, toItem:self, attribute:.right, multiplier:1, constant:-20)); self.addConstraint(NSLayoutConstraint(item:toastLabel, attribute:.bottom, relatedBy:.equal, toItem:self, attribute:.bottom, multiplier:1, constant:-20)); self.addConstraint(NSLayoutConstraint(item:toastLabel, attribute:.centerX, relatedBy:.equal, toItem:self, attribute:.centerX, multiplier:1, constant:0)); UIView.animate(withDuration:0.5, delay:Double(duration) / 1000.0, options:[], animations: { toastLabel.alpha = 0.0; }) { (Bool) in toastLabel.removeFromSuperview(); } } } 

UIPaddingLabel类:

 import UIKit @IBDesignable class UIPaddingLabel: UILabel { private var _padding:CGFloat = 0.0; public var padding:CGFloat { get { return _padding; } set { _padding = newValue; paddingTop = _padding; paddingLeft = _padding; paddingBottom = _padding; paddingRight = _padding; } } @IBInspectable var paddingTop:CGFloat = 0.0; @IBInspectable var paddingLeft:CGFloat = 0.0; @IBInspectable var paddingBottom:CGFloat = 0.0; @IBInspectable var paddingRight:CGFloat = 0.0; override func drawText(in rect: CGRect) { let insets = UIEdgeInsets(top:paddingTop, left:paddingLeft, bottom:paddingBottom, right:paddingRight); super.drawText(in: UIEdgeInsetsInsetRect(rect, insets)); } override var intrinsicContentSize: CGSize { get { var intrinsicSuperViewContentSize = super.intrinsicContentSize; intrinsicSuperViewContentSize.height += paddingTop + paddingBottom; intrinsicSuperViewContentSize.width += paddingLeft + paddingRight; return intrinsicSuperViewContentSize; } } } 

丹尼尔D有一个优雅的解决scheme,但UIAlertView已弃用。 改用UIAlertController:

  NSString *message = @"Toast message."; UIAlertController *toast =[UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert]; [self presentViewController:toast animated:YES completion:nil]; int duration = 2; // in seconds dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ [toast dismissViewControllerAnimated:YES completion:nil]; }); 

这个也很方便,因为它也有一个完成块,请看看:) https://github.com/PrajeetShrestha/EkToast

我知道这个问题很老,但是我发现自己在这里想知道同样的事情,于是我find了一个解决scheme,所以我想我会分享。 这种方法允许警报在您设定的时间延迟后被解散。

 let alertController = UIAlertController(title: "Error", message: "There was a problem logging in, please try again", preferredStyle: UIAlertControllerStyle.alert) self.present(alertController, animated: true, completion: nil) let delay = DispatchTime.now() + 1 // change 1 to desired number of seconds DispatchQueue.main.asyncAfter(deadline: delay) { // Your code with delay alertController.dismiss(animated: true, completion: nil) } 

如果出现崩溃应用程序的错误,可能是因为alertConroller正在后台线程上运行。 为了解决这个问题,你的代码在这里:

 DispatchQueue.main.async(execute: { }); 

这种方法允许消息在用户点击消息下方的“OK”button时被解除

 let alertController = UIAlertController(title: "Error", message: "There was a problem logging in, please try again", preferredStyle: UIAlertControllerStyle.alert) let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (result : UIAlertAction) -> Void in print("OK") } alertController.addAction(okAction) self.present(alertController, animated: true, completion: nil) 

你可以使用,我一直使用这个,这在目标c中完美地工作。

 +(void)showToastOnView:(UIView * _Nonnull)view withString:(NSString * _Nonnull)text forDuration:(AVToastDurationStatus)duration; 

这里提供:

AviToast Github。