iOS:查看可见但未激活

时间:2017-05-19 08:42:32

标签: ios uiview uiviewcontroller xamarin.ios

我有UIViewController初始化UIView。 此视图包含UITextFieldUIButton等互动元素。 视图添加在方法底部的ViewDidLoad上,以确保当我看到它时,可以通过用户交互来访问。

但是当我显示视图时,无法在此视图上进行任何交互。

这是唯一可能的吗?我做错了吗?

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

1 个答案:

答案 0 :(得分:2)

没有理由你不能这样做。 您可以像

那样构建代码
  • 的UIViewController
    • 控制器1
    • 控制器2
    • ...
    • 视图1
    • 视图2
    • ...

然后在任何Controller中使用View1和View2。

我尝试了你的代码,我正确地弹出了所有东西。 正如您所说,如果视图显示在屏幕上,但是来自下方的另一个视图中的元素与用户交互,则您可以尝试将视图置于最前面。

试试这个

View.BringSubviewToFront(AddBusinessEventView);

在ViewDidLoad函数的末尾。