我正在为Xamarin(C#)编写iOS应用程序。
在iOS中,Android的“吐司通知”相当于什么?
Toast在小弹出窗口中提供有关操作的简单反馈。它仅填充消息所需的空间量,并且当前活动保持可见和交互。例如,在发送电子邮件之前导航远离电子邮件会触发“草稿保存”吐司,以便您知道以后可以继续编辑。超时后,Toasts会自动消失。
例如在Android(C#)中,我可以这样显示:
Toast.MakeText(this, "Added " + counttext.Text +" pcs to cart" , ToastLength.Long).Show();
如何为iOS编写相同内容?
感谢您的回答。
答案 0 :(得分:5)
在Xamarin iOS中,您必须使用自定义设计的UIView和动画来实现相同的效果
public void ShowToast(String message, UIView view)
{
UIView residualView = view.ViewWithTag(1989);
if (residualView != null)
residualView.RemoveFromSuperview();
var viewBack = new UIView(new CoreGraphics.CGRect(83, 0, 300, 100));
viewBack.BackgroundColor = UIColor.Black;
viewBack.Tag = 1989;
UILabel lblMsg = new UILabel(new CoreGraphics.CGRect(0, 20, 300, 60));
lblMsg.Lines = 2;
lblMsg.Text = message;
lblMsg.TextColor = UIColor.White;
lblMsg.TextAlignment = UITextAlignment.Center;
viewBack.Center = view.Center;
viewBack.AddSubview(lblMsg);
view.AddSubview(viewBack);
roundtheCorner(viewBack);
UIView.BeginAnimations("Toast");
UIView.SetAnimationDuration(3.0f);
viewBack.Alpha = 0.0f;
UIView.CommitAnimations();
}
答案 1 :(得分:2)
使用Xamarin内置组件BigTed
https://components.xamarin.com/view/btprogresshud
BTProgressHUD.ShowToast("Hello from Toast");
答案 2 :(得分:0)
从 iOS 9 开始,建议使用UIAlertController类向用户显示反馈。
使用示例:
var alertController = UIAlertController.Create("Your Count", "Added 1 PC", UIAlertControllerStyle.Alert);
alertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, (action) => Console.WriteLine("OK Clicked.")));
PresentViewController(alertController, true, null);
要记住的一件事是,您必须在UIViewController中才能显示提醒。这是因为 UIAlertController 是 UIViewController 的子类。
答案 3 :(得分:0)
您可以尝试 https://github.com/andrius-k/Toast
它作为nuget包Toast.ios提供
用法:
// import
using GlobalToast;
Toast.MakeToast("message").SetDuration(0.5).Show();