2017-11-07 10 views
0

テキストボックスがキーボードで覆われている場合、表示をスクロールするコードが少しあります。コールバックメソッドが 'KeyboardWillShow'であるXamarinの開発者ガイドの 'UIKeyboard.Notifications.ObserveWillShow'の例に示すように、メソッドスタイルを使用しています。ここに私の実装があります。UIKeyboardEventArgs FrameBegin最初のクリック後に高さが0を返します

public void KeyboardWillShow(UIKeyboardEventArgs KeyboardArgs, UIView uiResponderView) 
    { 
    if (ScrollView != null) 
      { 
       if (uiResponderView != null) 
       { 
        UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, KeyboardArgs.FrameBegin.Height, 0.0f); 
        ScrollView.ContentInset = contentInsets; 
        ScrollView.ScrollIndicatorInsets = contentInsets; 

        CGRect tableViewRect = ScrollView.Frame; 
        tableViewRect.Height -= KeyboardArgs.FrameBegin.Height; 

        if (!tableViewRect.Contains(uiResponderView.Frame.Location)) 
        { 
         ScrollView.ScrollRectToVisible(uiResponderView.Frame, true); 
        } 
       } 
      } 
    } 

私はまた、キーボードはコールバックメソッドは、「KeyboardWillHide」ですXamarinの開発者ガイドの「UIKeyboard.Notifications.ObserveWillHide」の例を使用して隠したときのために聞いています。ここにその実装があります。

public void KeyBoardWillHide(object sender, UIKeyboardEventArgs args) 
    { 
     ScrollView.ContentInset = UIEdgeInsets.Zero; 
     ScrollView.ScrollIndicatorInsets = UIEdgeInsets.Zero; 
    } 

このすべてが無問題で初めて動作しますが、後続のすべての時間「KeyboardArgs.FrameBegin.Heightが」0誰かが私が欠けているものを知って聞かせてもらえ返しますか?

編集: 「ViewWillDisappear」には、オブザーバーを処分します。

解決法: Kevinのメモに基づいて、 'KeyboardArgs.FrameBegin.Height'の代わりに 'KeyboardArgs.FrameEnd.Height'を使用するように 'KeyboardWillShow'イベントを変更しました。このプロセスは問題なく動作します。

public void KeyboardWillShow(UIKeyboardEventArgs KeyboardArgs, UIView uiResponderView) 
{ 
    if (ScrollView != null) 
    { 
     if (uiResponderView != null) 
     { 
      UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, KeyboardArgs.FrameEnd.Height, 0.0f); 
      ScrollView.ContentInset = contentInsets; 
      ScrollView.ScrollIndicatorInsets = contentInsets; 

      CGRect tableViewRect = ScrollView.Frame; 
      tableViewRect.Height -= KeyboardArgs.FrameEnd.Height; 

      if (!tableViewRect.Contains(uiResponderView.Frame.Location)) 
      { 
       ScrollView.ScrollRectToVisible(uiResponderView.Frame, true); 
      } 
     } 
    } 
} 

答えて

1

ソリューション:

利用FrameEnd.Heightの代わりFrameBegin.Heightイベントは今のように見えます。

参照:

UIKeyboardFrameBeginUserInfoKey

スクリーン座標、キーボードの開始フレーム矩形を識別するCGRectを含むNSValueオブジェクトのキー。フレームの四角形は、デバイスの現在の向きを反映しています。

UIKeyboardFrameEndUserInfoKey

画面座標におけるキーボードの終了フレーム矩形を識別するCGRectを含むNSValueオブジェクトのキー。フレームの四角形は、デバイスの現在の向きを反映しています。

Appleの文書: https://developer.apple.com/documentation/uikit/uikeyboardframebeginuserinfokey https://developer.apple.com/documentation/uikit/uikeyboardframeenduserinfokey

その他の関連ケース:iOS 11 - Keyboard Height is returning 0 in keyboard notification

関連する問題