How to add subview above the keyboard in monotouch

2019-08-17 05:48发布

问题:

How can i add a UIView on top of the keyboard in monotouch/Xamarin.Ios?

i have tried the following but it doesn't seem to work.

UIApplication.SharedApplication.Delegate.Window.AddSubview (myView);
UIApplication.SharedApplication.Delegate.Window.BringSubviewToFront (myView);

The UIView is added behind the keyboard.

In Objective c i was able to use the following code and it worked.

[[[[UIApplication sharedApplication] windows] objectAtIndex:1] addSubview:myView];

Thank You.

回答1:

You'll first need to create a new UIWindow at the same level as status bar. You can put this code in your ViewDidLoad override:

newWindow = new UIWindow (UIScreen.MainScreen.Bounds);
newWindow.WindowLevel = UIWindow.LevelStatusBar;

And then add your View (in this case a button) to the new UIWindow:

var uIButton = new UIButton (UIButtonType.InfoDark) {
    Frame = new RectangleF (10, 400, 30, 30)
};
newWindow.AddSubview (uIButton);

In the UITextField, add an event listener for the EditingDidBegin

MyInput.EditingDidBegin += (object sender, EventArgs e) => {
    newWindow.Hidden = false;
};


回答2:

This Worked for me.

UIApplication.SharedApplication.Windows[1].AddSubview(myView);