我正在Xamarin(C#)中编写iOS的应用程序。
在iOS中,Android的“toast通知”相当于什么?
从documentation:
例如,在Android(C#)中,我可以像这样显示它们:
Toast.MakeText(this, "Added " + counttext.Text +" pcs to cart" , ToastLength.Long).Show();
如何为iOS编写相同的代码?
感谢您的回答。
最佳答案
在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();
}