IOS官方的Object-C有例子,但是OC的代码。Moving Content That Is Located Under the Keyboard

效果如下图:

Monotouch 移动位于键盘下的内容,自动滚动被键盘遮住的内容

实现方法:

1.定义一个所有ViewControl的基类,命名为ViewControllerBase,ViewControl继承ViewControllerBase

代码如下:

 

class ViewControllerBase : UIViewController
   2: {
   3:     NSObject _keyboardObserverWillShow;
   4:     NSObject _keyboardObserverWillHide;
   5:  
public ViewControllerBase()
   7:     {
   8:     }
base(handle)
  10:     {
  11:  
  12:     }
  13:  
void ViewDidLoad()
  15:     {
//设置键盘事件处理程序
  17:         RegisterForKeyboardNotifications();
  18:     }
  19:  
void RegisterForKeyboardNotifications()
  21:     {
  22:         _keyboardObserverWillShow = NSNotificationCenter.DefaultCenter.AddObserver
  23:             (UIKeyboard.WillShowNotification, KeyboardWillShowNotification);
  24:  
  25:         _keyboardObserverWillHide = NSNotificationCenter.DefaultCenter.AddObserver
  26:             (UIKeyboard.WillHideNotification, KeyboardWillHideNotification);
  27:     }
  28:  
void UnregisterKeyboardNotifications()
  30:     {
  31:         NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardObserverWillShow);
  32:         NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardObserverWillHide);
  33:     }
  34:  
virtual UIView KeyboardGetActiveView()
  36:     {
this.View.FindFirstResponder();
  38:     }
  39:  
void KeyboardWillShowNotification(NSNotification notification)
  41:     {
  42:         UIView activeView = KeyboardGetActiveView();
null)
return;
  45:  
as UIScrollView;
null)
return;
  49:  
  50:         RectangleF keyboardBounds = UIKeyboard.BoundsFromNotification(notification);
  51:  
new UIEdgeInsets(0.0f, 0.0f, keyboardBounds.Size.Height, 0.0f);
  53:         scrollView.ContentInset = contentInsets;
  54:         scrollView.ScrollIndicatorInsets = contentInsets;
  55:  
this.View.Frame.Location,
this.View.Frame.Size.Height - keyboardBounds.Size.Height));
  58:  
this.View);
  60:  
if (!viewRectAboveKeyboard.Contains(activeFieldAbsoluteFrame))
  62:         {
new PointF(0.0f, 
  64:                 activeFieldAbsoluteFrame.Location.Y + activeFieldAbsoluteFrame.Height
  65:                 + scrollView.ContentOffset.Y - viewRectAboveKeyboard.Height);
true);
  67:         }
  68:     }
  69:  
void KeyboardWillHideNotification(NSNotification notification)
  71:     {
  72:         UIView activeView = KeyboardGetActiveView();
null)
return;
  75:  
as UIScrollView;
null)
return;
  79:  
double animationDuration = UIKeyboard.AnimationDurationFromNotification(notification);
new UIEdgeInsets(0.0f, 0.0f, 0.0f, 0.0f);
delegate
  83:         {
  84:             scrollView.ContentInset = contentInsets;
  85:             scrollView.ScrollIndicatorInsets = contentInsets;
  86:         });
  87:     }
  88: }

相关文章:

  • 2021-09-08
  • 2022-12-23
  • 2021-08-28
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-29
  • 2021-12-19
  • 2021-07-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案