3

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.

Khaksar
  • 333
  • 3
  • 17

1 Answers1

3

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/

dr_barto
  • 5,723
  • 3
  • 26
  • 47
KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • "BoundsFromNotification" is depricated. Its not working. What is "activeview"? Is this the id of the UIView that came with my UIViewController? My UIView Name & Class fields are empty, i checked this from the properties page of my UIView. What to do next? I got some information from below link: [link](https://developer.xamarin.com/api/member/MonoTouch.UIKit.UIKeyboard+Notifications.ObserveWillShow/) – Khaksar Dec 14 '16 at 13:57
  • activeview is your tracking view for that see my updated answer . – KKRocks Dec 15 '16 at 04:57
  • try this for avoid deprecation : http://stackoverflow.com/a/7785126/3901620 – KKRocks Dec 15 '16 at 04:58
  • I am new in this, can you please give me something complete which I can use easily? – Khaksar Dec 15 '16 at 18:54
  • What is "ScrollTheView (moveViewUp);" I didn't find any method with name **ScrollTheView** ? – Khaksar Dec 16 '16 at 04:41