Khaksar
Khaksar

Reputation: 343

Keyboard is hiding UITextView in UIViewController Xamarin IOS

I created a UIViewController and Put a Label and UITextView on UIViewController.

I want to put some UITextViews on my UIViewController one under another. I am using Landscape mode only in my App. I am using Xamarin IOS for making this app.

Below screen shows what is happening! Can someone please help me!

Showing UITextView.

Keyboard is hiding my textview field.

Upvotes: 3

Views: 2345

Answers (1)

KKRocks
KKRocks

Reputation: 8322

You need to add observer in your viewcontroller where this issue occurs as below .

Keyboard observers for ViewDidLoad().

// Keyboard popup
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.DidShowNotification,KeyBoardUpNotification);

// Keyboard Down
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.WillHideNotification,KeyBoardDownNotification);

// Keyboard popup
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.DidShowNotification,KeyBoardUpNotification);

// Keyboard Down
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.WillHideNotification,KeyBoardDownNotification);

First up is the KeyboardUpNotification method. Essentially you calculate if the control will be hidden by the keyboard and if so calculate how much the view needs to be moved to show the control, and then move it.

private void KeyBoardUpNotification(NSNotification notification)
{
    // get the keyboard size
    RectangleF r = UIKeyboard.BoundsFromNotification (notification);

    // Find what opened the keyboard
    foreach (UIView view in this.View.Subviews) {
        if (view.IsFirstResponder)
            activeview = view;
    }

    // Bottom of the controller = initial position + height + offset      
    bottom = (activeview.Frame.Y + activeview.Frame.Height + offset);

    // Calculate how far we need to scroll
    scroll_amount = (r.Height - (View.Frame.Size.Height - bottom)) ;

    // Perform the scrolling
    if (scroll_amount > 0) {
         moveViewUp = true;
         ScrollTheView (moveViewUp);
    } else {
         moveViewUp = false;
    }

}

private void KeyBoardUpNotification(NSNotification notification)
{
    // get the keyboard size
    RectangleF r = UIKeyboard.BoundsFromNotification (notification);

    // Find what opened the keyboard
    foreach (UIView view in this.View.Subviews) {
        if (view.IsFirstResponder)
            activeview = view;
    }

    // Bottom of the controller = initial position + height + offset      
    bottom = (activeview.Frame.Y + activeview.Frame.Height + offset);

    // Calculate how far we need to scroll
    scroll_amount = (r.Height - (View.Frame.Size.Height - bottom)) ;

    // Perform the scrolling
    if (scroll_amount > 0) {
         moveViewUp = true;
         ScrollTheView (moveViewUp);
    } else {
         moveViewUp = false;
    }
}

Active field is use for track your currently started textfield.

public override void EditingStarted (UITextField textField)
{
    activeview = textField;
}

for more : http://www.gooorack.com/2013/08/28/xamarin-moving-the-view-on-keyboard-show/

Upvotes: 3

Related Questions