Android Toast in iPhone?

后端 未结 12 825
轮回少年
轮回少年 2020-12-05 20:38

When I write Android apps, I love the Toast feature. Is there a way to get this kind of set and forget popup message in iPhone development using MonoTouch (C# .NET)?

12条回答
  •  有刺的猬
    2020-12-05 21:16

    I have added a little modification to the toast class that handles rotation of the display.

            public void Show ()
        {
            UIButton v = UIButton.FromType (UIButtonType.Custom);
            view = v;
    
    
            UIFont font = UIFont.SystemFontOfSize (16);
            SizeF textSize = view.StringSize (text, font, new SizeF (280, 60));
    
            UILabel label = new UILabel (new RectangleF (0, 0, textSize.Width + 5, textSize.Height + 5));
            label.BackgroundColor = UIColor.Clear;
            label.TextColor = UIColor.White;
            label.Font = font;
            label.Text = text;
            label.Lines = 0;
            label.ShadowColor = UIColor.DarkGray;
            label.ShadowOffset = new SizeF (1, 1);
    
    
            v.Frame = new RectangleF (0, 0, textSize.Width + 10, textSize.Height + 10);
            label.Center = new PointF (v.Frame.Size.Width / 2, v.Frame.Height / 2);
            v.AddSubview (label);
    
            v.BackgroundColor = UIColor.FromRGBA (0, 0, 0, 0.7f);
            v.Layer.CornerRadius = 5;
    
            UIWindow window = UIApplication.SharedApplication.Windows[0];
    
            PointF point = new PointF (window.Frame.Size.Width / 2, window.Frame.Size.Height / 2);
    
            if (theSettings.Gravity == ToastGravity.Top)
            {
                point = new PointF (window.Frame.Size.Width / 2, 45);
            }
            else if (theSettings.Gravity == ToastGravity.Bottom)
            {
                point = new PointF (window.Frame.Size.Width / 2, window.Frame.Size.Height - 45);
            }
            else if (theSettings.Gravity == ToastGravity.Center)
            {
                point = new PointF (window.Frame.Size.Width / 2, window.Frame.Size.Height / 2);
            }
            else
            {
                point = theSettings.Position;
            }
    
            point = new PointF (point.X + offsetLeft, point.Y + offsetTop);
            v.Center = point;
            //handle screen rotation
            float orientation=0;
    
            switch(UIApplication.SharedApplication.StatusBarOrientation)
            {
            case UIInterfaceOrientation.LandscapeLeft:
                orientation=-90;
                break;
            case UIInterfaceOrientation.LandscapeRight:
                orientation=90;
                break;
            case UIInterfaceOrientation.PortraitUpsideDown:
                orientation=180;
                break;
            }
            v.Transform=CGAffineTransform.MakeRotation ((float)(orientation / 180f * Math.Pi));
            window.AddSubview (v);
            v.AllTouchEvents += delegate { HideToast (); };
    
            NSTimer.CreateScheduledTimer (theSettings.DurationSeconds, HideToast);
    
        }
    

提交回复
热议问题