iOS : View visible but not active

标签 ios uiview uiviewcontroller xamarin.ios

我得到了 UIViewController初始化一个UIView 。 此 View 包含交互式元素,例如 UITextFieldUIButton 。 View 被添加到方法底部的 ViewDidLoad 上,以确保当我使其可见时,用户交互可以访问它。

但是当我显示 View 时,无法在此 View 上进行交互。

只有这个可能吗?我做错了什么吗?

The View
public class AddBusinessEventView : UIView
{
    public UILabel LblTitle;
    public UITextField TxtType;
    public UIButton BtnClose;

    public AddBusinessEventView(nfloat bw, nfloat bh)
    {


        //Bouton pour fermer le popup
        BtnClose = new UIButton();
        BtnClose.SetImage(UIImage.FromFile("Images/Boutons/lightbox_close.png"), UIControlState.Normal);
        BtnClose.Frame = new CGRect(bw - 80, 30, BtnFermer.ImageView.Image.CGImage.Width * 0.5, BtnFermer.ImageView.Image.CGImage.Height * 0.5);

        //Doit se trouver par dessus txtSite et ajouté après dans la vue pour se trouvé en premier plan

        LblTitle = new UILabel();
        LblTitle.Frame = new CGRect((bw - (lw + 200)) / 2, 100, lw + 200, 30);
        LblTitle.Text = "Fill with your event elements";
        LblTitle.Font = UIFont.FromName("GillSans-Bold", 22);
        LblTitle.TextColor = UIColor.FromRGB(211, 3, 67);
        LblTitle.TextAlignment = UITextAlignment.Center;

        TxtType = new UITextField(new CGRect((bw - 750) / 2, 140, 350, 40));
        TxtType.BackgroundColor = UIColor.White;
        TxtType.TextAlignment = UITextAlignment.Center;
        TxtType.BorderStyle = UITextBorderStyle.RoundedRect;
        TxtType.AutocorrectionType = UITextAutocorrectionType.No;
        TxtType.AutocapitalizationType = UITextAutocapitalizationType.AllCharacters;
        TxtType.Placeholder = "Type";

        AddSubviews(BtnClose, LblTitle, TxtType);
    }
}

UIViewController

partial class EvenementViewController : EnhancedUIViewController
{
    AddBusinessEventView AddBusinessEventView;

    public EvenementViewController(IntPtr handle) : base(handle) { }

    public EvenementViewController() : base() { }

    public override void ViewDidAppear(bool animated)
    {
        base.ViewDidAppear(animated);
        if (myEvent == null)
        {
            ShowAddBusinessEventView();
        }
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        nfloat bw = View.Bounds.Width;
        nfloat bh = View.Bounds.Height;
        //Another Elements are adding to view here
        //...

        AddBusinessEventView = new AddBusinessEventView(bw, bh);
        AddBusinessEventView.Hidden = true;

        //Much more View.Add with all elements here
        //...

        View.Add(AddBusinessEventView);
        AddBusinessEventView.BtnType.TouchUpInside += BtnClose_TouchUpInside;
    }

    #region BusinessEventAdd
    void ShowAddBusinessEventView()
    {
        UIView.Animate(duration: 1,
            delay: 0,
            options: UIViewAnimationOptions.CurveEaseInOut,
            animation: () =>
            {
                AddBusinessEventView.Alpha = 1.0f;
            },
            completion: () =>
            {
                AddBusinessEventView.Hidden = false;
                AddBusinessEventListener();
            }

        );
    }

    void HideAddBusinessEventView()
    {
        UIView.Animate(duration: 1,
            delay: 0,
            options: UIViewAnimationOptions.CurveEaseInOut,
            animation: () =>
            {
                AddBusinessEventView.Alpha = 0.0f;
            },
            completion: () =>
            {
                AddBusinessEventView.Hidden = true;
                RemoveBusinessEventListener();
            }

        );
    }

    void BtnClose_TouchUpInside(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.Print("Touching myself");
    }
    #endregion
}

请考虑将EnhancedViewController作为标准UIViewController,我只是添加一些增强功能来向用户显示来自Overlay的消息。

正如我所说,我们既不能与 TxtType 也不能与 BtnClose 交互。

编辑: 不确定是否有帮助;但是当 View添加到主View关于UIViewController ,它显示得很好,但所有用户交互都是在此 View 下的元素上捕获的 即:AddBusinessEventView作为弹出窗口,因此当我按下一个元素时,它覆盖了所有其他元素,如果另一个元素添加在此之前 View在下面,是这个元素而不是AddBusinessEventView捕获触摸事件的元素。

如上所述,主要目的是将 View 元素剪切到不同的文件上,以提高该应用程序的可读性和可维护性。 enter code here

最佳答案

您没有理由不能这样做。 您可以像这样构建您的代码

  • UIViewController
    • Controller 1
    • Controller 2
    • ...
  • 观看次数
    • 查看1
    • 查看2
    • ...

然后在任意一个Controller中使用View1和View2。

我尝试了你的代码,并且我正确地弹出了所有内容。 正如您所说,如果 View 显示在屏幕上,但它是来自下面与用户交互的另一个 View 的元素,也许您可​​以尝试将 View 置于前面。

试试这个

View.BringSubviewToFront(AddBusinessEventView);

在 ViewDidLoad 函数的末尾。

关于iOS : View visible but not active,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44065320/

相关文章:

ios - 使用 EventKit 创建提醒时出错

ios - 当用户点击 View Controller 时如何使 SWRevealViewController 菜单消失

iphone - 如何在没有 Interface Builder 的情况下在 UIViewController 中实现 UISearchDisplayController

ios - 如何从 AppDelegate 实例化 UIViewController?

ios - 将 IMEI 解码为序列号

ios - 从 iPhone 上的 Exchange IMAP 邮箱中删除邮件

ios - 无法摆脱致命异常 : NSInternalInconsistencyException Error

swift - UIView 子类 (Swift4) - 自定义方法 (setNumber) 未按预期工作

ios - 防止快速点击 View

ios - 访问 Storyboard 中的 View