回答

0

參見樣品執行力度

變量用於容納所述文本框

private UIView activeview;    // Controller that activated the keyboard 
private float scroll_amount = 0.0f; // amount to scroll 
private float bottom = 0.0f;   // bottom point 
private float offset = 10.0f;   // extra offset 
private bool moveViewUp = false;   // which direction are we moving 

鍵盤觀察員viewDidLoad中()中的控制器。

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

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

首先是KeyboardUpNotification方法。實際上,您可以計算控件是否被鍵盤隱藏,如果是,則計算視圖需要移動多少以顯示控件,然後移動它。

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 KeyBoardDownNotification(NSNotification notification) 
{ 
    if(moveViewUp){ScrollTheView(false);} 
} 

滾動視圖將以動畫方式滾動視圖。

private void ScrollTheView(bool move) 
{ 

    // scroll the view up or down 
    UIView.BeginAnimations (string.Empty, System.IntPtr.Zero); 
    UIView.SetAnimationDuration (0.3); 

    RectangleF frame = View.Frame; 

    if (move) { 
     frame.Y -= scrollamount; 
    } else { 
     frame.Y += scrollamount; 
     scrollamount = 0; 
    } 

    View.Frame = frame; 
    UIView.CommitAnimations(); 
} 
+0

嗨,我用您的解決方案,並遇到了一個問題。我的計算scroll_amount總是消極的...任何想法我做錯了什麼... Thx – Christoph