public static void TPKeyboardAvoiding_updateContentInset(this UIScrollView scrollView)
        {
            //TPKeyboardAvoidingState state = KeyboardAvoidingState ();
            if (State == null)
            {
                State = new TPKeyboardAvoidingState();
            }

            if (State.keyboardVisible)
            {
                scrollView.ContentInset = scrollView.TPKeyboardAvoiding_contentInsetForKeyboard();
            }
        }
        public static UIEdgeInsets TPKeyboardAvoiding_contentInsetForKeyboard(this UIScrollView scrollView)
        {
            //TPKeyboardAvoidingState state = KeyboardAvoidingState();
            if (State == null)
            {
                State = new TPKeyboardAvoidingState();
            }

            UIEdgeInsets newInset     = scrollView.ContentInset;
            CGRect       keyboardRect = State.keyboardRect;

            newInset.Bottom = (nfloat)(keyboardRect.Size.Height - Math.Max((keyboardRect.GetMaxY() - scrollView.Bounds.GetMaxY()), 0));
            return(newInset);
        }
        public static void TPKeyboardAvoiding_updateFromContentSizeChange(this UIScrollView scrollView)
        {
            //TPKeyboardAvoidingState state = KeyboardAvoidingState ();
            if (State == null)
            {
                State = new TPKeyboardAvoidingState();
            }

            if (State.keyboardVisible)
            {
                State.priorContentSize  = scrollView.ContentSize;
                scrollView.ContentInset = scrollView.TPKeyboardAvoiding_contentInsetForKeyboard();
            }
        }
        public static void TPKeyboardAvoiding_keyboardWillHide(this UIScrollView scrollView, NSNotification notification)
        {
            CGRect keyboardRect = scrollView.ConvertRectFromView((notification.UserInfo.ObjectForKey(UIKeyboard.FrameEndUserInfoKey) as NSValue).CGRectValue, null);

            if (keyboardRect == CGRect.Empty)
            {
                return;
            }

            //TPKeyboardAvoidingState state = KeyboardAvoidingState();
            if (State == null)
            {
                State = new TPKeyboardAvoidingState();
            }

            if (!State.keyboardVisible)
            {
                return;
            }

            State.keyboardRect    = new CGRect(0, 0, 0, 0);
            State.keyboardVisible = false;

            // Restore dimensions to prior size
            //TODO : add animations
            UIView.BeginAnimations(null);
            UIView.SetAnimationCurve(UIViewAnimationCurve.EaseIn);
            UIView.SetAnimationDuration(1.0);

            if (scrollView is TPKeyboardAvoidingScrollView)
            {
                scrollView.ContentSize = State.priorContentSize;
            }

            scrollView.ContentInset          = State.priorInset;
            scrollView.ScrollIndicatorInsets = State.priorScrollIndicatorInsets;
            scrollView.PagingEnabled         = State.priorPagingEnabled;
            scrollView.LayoutIfNeeded();

            UIView.CommitAnimations();
        }
        public static void TPKeyboardAvoiding_scrollToActiveTextField(this UIScrollView scrollView)
        {
            //TPKeyboardAvoidingState state = KeyboardAvoidingState ();
            if (State == null)
            {
                State = new TPKeyboardAvoidingState();
            }

            if (!State.keyboardVisible)
            {
                return;
            }

            nfloat visibleSpace = scrollView.Bounds.Size.Height - scrollView.ContentInset.Top - scrollView.ContentInset.Bottom;

            CGPoint idealOffset = new CGPoint(0, scrollView.TPKeyboardAvoiding_idealOffsetForViewwithViewingAreaHeight(scrollView.TPKeyboardAvoiding_findFirstResponderBeneathView(scrollView), visibleSpace));

            DispatchQueue.CurrentQueue.DispatchAfter(DispatchTime.Now, () =>
            {
                scrollView.SetContentOffset(idealOffset, true);
            });
        }
        /*static TPKeyboardAvoidingState KeyboardAvoidingState()
         * {
         *      TPKeyboardAvoidingState state = State;
         *      if(state == null)
         *              state = new TPKeyboardAvoidingState();
         *
         *      return state;
         * }*/

        public static void TPKeyboardAvoiding_keyboardWillShow(this UIScrollView scrollView, NSNotification notification)
        {
            CGRect keyboardRect = scrollView.ConvertRectFromView((notification.UserInfo.ObjectForKey(UIKeyboard.FrameEndUserInfoKey) as NSValue).CGRectValue, null);

            if (keyboardRect == CGRect.Empty)
            {
                return;
            }

            //TPKeyboardAvoidingState state = KeyboardAvoidingState();
            if (State == null)
            {
                State = new TPKeyboardAvoidingState();
            }

            UIView firstResponder = scrollView.TPKeyboardAvoiding_findFirstResponderBeneathView(scrollView);

            if (firstResponder == null)
            {
                return;
            }

            State.keyboardRect = keyboardRect;

            if (!State.keyboardVisible)
            {
                State.priorInset = scrollView.ContentInset;
                State.priorScrollIndicatorInsets = scrollView.ScrollIndicatorInsets;
                State.priorPagingEnabled         = scrollView.PagingEnabled;
            }

            State.keyboardVisible    = true;
            scrollView.PagingEnabled = false;

            if (scrollView is TPKeyboardAvoidingScrollView)
            {
                State.priorContentSize = scrollView.ContentSize;

                if (CGSize.Equals(scrollView.ContentSize, new CGSize(0, 0)))
                {
                    // Set the content size, if it's not set. Do not set content size explicitly if auto-layout
                    // is being used to manage subviews
                    scrollView.ContentSize = scrollView.TPKeyboardAvoiding_calculatedContentSizeFromSubviewFrames();
                }
            }

            // Shrink view's inset by the keyboard's height, and scroll to show the text field/view being edited
            //TODO : need fix
            UIView.BeginAnimations(null);
            NSNumber number = notification.UserInfo.ValueForKey(UIKeyboard.AnimationCurveUserInfoKey) as NSNumber;

            UIView.SetAnimationCurve((UIViewAnimationCurve)number.Int32Value);
            number = notification.UserInfo.ValueForKey(UIKeyboard.AnimationDurationUserInfoKey) as NSNumber;
            UIView.SetAnimationDuration(number.FloatValue);
            //          UIView.SetAnimationCurve (UIViewAnimationCurve.EaseIn);
            //          UIView.SetAnimationDuration (1.0);

            scrollView.ContentInset = scrollView.TPKeyboardAvoiding_contentInsetForKeyboard();

            nfloat viewableHeight = scrollView.Bounds.Size.Height - scrollView.ContentInset.Top - scrollView.ContentInset.Bottom;

            scrollView.SetContentOffset(new CGPoint(scrollView.ContentOffset.X, (nfloat)scrollView.TPKeyboardAvoiding_idealOffsetForViewwithViewingAreaHeight(firstResponder, viewableHeight)), false);

            scrollView.ScrollIndicatorInsets = scrollView.ContentInset;
            scrollView.LayoutIfNeeded();

            UIView.CommitAnimations();
        }