Add UITextView on UIAlertView (iOS, Xamarin) without using any third party lib

前端 未结 2 997
一整个雨季
一整个雨季 2021-01-07 05:56

I want to display a dialog in iOS and add a UITextView to the view of the dialog. The UITextView textView has text with clickable links for email,p

相关标签:
2条回答
  • 2021-01-07 06:01

    Seems like you need a custom popup view to finish what you need, I write a sample for you, this is the code:

    This is the ViewController.cs:

    public partial class ViewController : UIViewController
    {
        public ViewController (IntPtr handle) : base (handle)
        {
        }
    
        public override void ViewDidLoad ()
        {
            this.View.BackgroundColor = UIColor.Gray;
            this.View.AddGestureRecognizer (new UITapGestureRecognizer (() => {
                CustomPopUpView cpuv = new CustomPopUpView (new CoreGraphics.CGSize (300, 200));
                cpuv.PopUp (true,delegate{
                    Console.WriteLine("cpuv will close");
                });
            }));
        }
    }
    

    And this is the CustomPopUpView.cs:

    public class CustomPopUpView : UIView
    {
        public delegate void PopWillCloseHandler ();
        public event PopWillCloseHandler PopWillClose;
    
        private UIVisualEffectView effectView = new UIVisualEffectView (UIBlurEffect.FromStyle (UIBlurEffectStyle.Dark));
        private UIButton btnClose = new UIButton (UIButtonType.System);
    
        public CustomPopUpView (CGSize size)
        {
            nfloat lx = (UIScreen.MainScreen.Bounds.Width - size.Width) / 2;
            nfloat ly = (UIScreen.MainScreen.Bounds.Height - size.Height) / 2;
            this.Frame = new CGRect (new CGPoint (lx, ly), size);
    
            effectView.Alpha = 0;
    
            this.BackgroundColor = UIColor.White;
    
            nfloat btnHeight = 40;
            btnClose.SetTitle ("Close", UIControlState.Normal);
            btnClose.Frame = new CGRect (0, this.Frame.Height - btnHeight, this.Frame.Width, btnHeight);
            btnClose.TouchUpInside += delegate {
                Close();
            };
            this.AddSubview (btnClose);
        }
    
        public void PopUp (bool animated = true, Action popAnimationFinish = null)
        {
            UIWindow window = UIApplication.SharedApplication.KeyWindow;
            effectView.Frame = window.Bounds;
            window.EndEditing (true);
            window.AddSubview (effectView);
            window.AddSubview (this);
    
            if (animated) {
                Transform = CGAffineTransform.MakeScale (0.1f, 0.1f);
                UIView.Animate (0.15, delegate {
                    Transform = CGAffineTransform.MakeScale (1, 1);
                    effectView.Alpha = 0.8f;
                },delegate {
                    if(null != popAnimationFinish)
                        popAnimationFinish ();
                });
            } else {
                effectView.Alpha = 0.8f;
            }
        }
    
        public void Close (bool animated = true)
        {
            if (animated) {
                UIView.Animate (0.15, delegate {
                    Transform = CGAffineTransform.MakeScale (0.1f, 0.1f);
                    effectView.Alpha = 0;
                }, delegate {
                    this.RemoveFromSuperview();
                    effectView.RemoveFromSuperview();
                    if(null != PopWillClose) PopWillClose ();
                });
            } else {
                if(null != PopWillClose) PopWillClose ();
            }
        }
    }
    

    You can just add anything you want to this CustomPopUpView, or you can inherit it and create your own popup.

    Hope it can help you.

    0 讨论(0)
  • 2021-01-07 06:10

    This is how you can add a UITextField in a UIAlertViewController

      var alert = UIAlertController.Create(Enter OTP, "", UIAlertControllerStyle.Alert);
                alert.AddTextField((obj) =>
                {
                    obj.Font = UIFont.SystemFontOfSize(20);
                    obj.TextAlignment = UITextAlignment.Center;
                    obj.Placeholder = "OTP";
    
                });
    
                alert.AddAction(UIAlertAction.Create("Submit", UIAlertActionStyle.Default, (obj) => 
                {
                    chargeReference = reference;
                    statusValue = alert.TextFields[0].Text;
                    CompleteFunding();
                }));
                PresentViewController(alert, true, null);
    

    I hope this helps

    0 讨论(0)
提交回复
热议问题