在 UIAlertView(iOS、Xamarin)上添加 UITextView,而不使用任何第三方库

Posted

技术标签:

【中文标题】在 UIAlertView(iOS、Xamarin)上添加 UITextView,而不使用任何第三方库【英文标题】:Add UITextView on UIAlertView (iOS, Xamarin) without using any third party lib 【发布时间】:2016-08-24 20:54:51 【问题描述】:

我想在 ios 中显示一个对话框并将UITextView 添加到对话框的视图中。 UITextView textView 的文本带有可点击的电子邮件、电话和网址链接。我可以触发对话框,但只能使用标题和确定按钮。对话框视图中没有添加任何文本视图。我不确定这里的问题是文本视图还是视图添加到对话框的方式。

请检查我下面的代码:

 UITextView textView = new UITextView();
 textView.Editable = false;
 textView.DataDetectorTypes = UIDataDetectorType.All;
 textView.Message = message;
 var dlg = UIAlertController.Create(Title ?? String.Empty, null, UIAlertControllerStyle.Alert);
 dlg.View.AddSubview(textView);
 dlg.AddAction(UIAlertAction.Create(OkText, UIAlertActionStyle.Default, action => Submit()));                

 var top = Utils.GetTopViewController();
 top.PresentViewController(dlg, animated: true, completionHandler: null);

谢谢,

【问题讨论】:

***.com/questions/23285750/…的可能重复 【参考方案1】:

好像你需要一个自定义的弹出视图来完成你需要的,我为你写了一个示例,代码如下:

这是 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");
            );
        ));
    

这是 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 ();
        
    

您可以向此 CustomPopUpView 添加任何您想要的内容,或者您​​可以继承它并创建自己的弹出窗口。

希望对你有帮助。

【讨论】:

当我关闭弹出窗口时,屏幕上仍然有小的白色区域,我将 Transform = CGAffineTransform.MakeScale (0.1f, 0.1f); 更改为 Transform = CGAffineTransform.MakeScale (0.01f, 0.01f); 很抱歉,我忘记删除了,关闭动画完成后你应该删除它,我已经编辑了我的代码,谢谢你提醒。@FahadRehman【参考方案2】:

这是在 UIAlertViewController 中添加 UITextField 的方法

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

希望对你有帮助

【讨论】:

以上是关于在 UIAlertView(iOS、Xamarin)上添加 UITextView,而不使用任何第三方库的主要内容,如果未能解决你的问题,请参考以下文章

UIAlertView - 在 IOS 中将消息设置为左对齐

如何在 iOS 8 中使用 UIAlertView? [复制]

ios开发之oc-UIAlertView

UIAlertView 已弃用:首先在 iOS 9.0 中弃用 - UIAlertView 已弃用。将 UIAlertController 与首选样式一起使用

iOS:简单使用UIAlertVIew和UIActionSheet

iOS自定义提示弹出框(类似UIAlertView)