惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Heimdal Security Blog
A
Arctic Wolf
K
Kaspersky official blog
V
Vulnerabilities – Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
L
LINUX DO - 热门话题
MongoDB | Blog
MongoDB | Blog
T
Threat Research - Cisco Blogs
D
Docker
爱范儿
爱范儿
T
Tenable Blog
C
Check Point Blog
B
Blog
C
Cisco Blogs
Vercel News
Vercel News
The Cloudflare Blog
T
Threatpost
NISL@THU
NISL@THU
T
Tor Project blog
V2EX - 技术
V2EX - 技术
P
Palo Alto Networks Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tailwind CSS Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
SecWiki News
SecWiki News
博客园 - 司徒正美
S
Security @ Cisco Blogs
GbyAI
GbyAI
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
The Register - Security
The Register - Security
Recorded Future
Recorded Future
Cloudbric
Cloudbric
Webroot Blog
Webroot Blog
N
News and Events Feed by Topic
Y
Y Combinator Blog
博客园_首页
T
Troy Hunt's Blog
The Hacker News
The Hacker News
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
U
Unit 42
AWS News Blog
AWS News Blog
PCI Perspectives
PCI Perspectives
V
Visual Studio Blog
博客园 - 聂微东
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - Bruce Lee

Android的界面设计规范-7 Android的界面设计规范-6 Android的界面设计规范-5 Android的界面设计规范-4 Android的界面设计规范-3 Android的界面设计规范-2 Android的界面设计规范-1 RESTful Web Services,客户端Silverlight提交POST数据报错 Monotouch Copy item from album(从相册拷贝文件出来) Monotouch Save Image To Application(保存相册图片到你的应用程序) Monotouch中使用UINavigationController Monotouch 项目选择Storyborads还是XIBs Montouch 定义一个定制的 View Control类 Monotouch 视图管理周期 Montouch多视图的创建与销毁 Monotouch 常用例子代码 Monotouch在IPAD与IPhone使用UIImagePickerController的图片选择不同的代码 Montouch 增加图片到模拟器(Simulator)的相册中 Monotouch Table View 里面动态增加Table View Cell Switch 触发Switch事件 程序崩溃
Monotouch 移动位于键盘下的内容,自动滚动被键盘遮住的内容
Bruce Lee · 2012-11-06 · via 博客园 - Bruce Lee

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

效果如下图:

滚动键盘下的内容

实现方法:

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

代码如下:

   1: public class ViewControllerBase : UIViewController
   2: {
   3:     NSObject _keyboardObserverWillShow;
   4:     NSObject _keyboardObserverWillHide;
   5:  
   6:     public ViewControllerBase()
   7:     {
   8:     }
   9:     public ViewControllerBase(IntPtr handle) : base(handle)
  10:     {
  11:  
  12:     }
  13:  
  14:     public override void ViewDidLoad()
  15:     {
  16:         //设置键盘事件处理程序
  17:         RegisterForKeyboardNotifications();
  18:     }
  19:  
  20:     protected virtual void RegisterForKeyboardNotifications()
  21:     {
  22:         _keyboardObserverWillShow = NSNotificationCenter.DefaultCenter.AddObserver
  23:             (UIKeyboard.WillShowNotification, KeyboardWillShowNotification);
  24:  
  25:         _keyboardObserverWillHide = NSNotificationCenter.DefaultCenter.AddObserver
  26:             (UIKeyboard.WillHideNotification, KeyboardWillHideNotification);
  27:     }
  28:  
  29:     protected virtual void UnregisterKeyboardNotifications()
  30:     {
  31:         NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardObserverWillShow);
  32:         NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardObserverWillHide);
  33:     }
  34:  
  35:     protected virtual UIView KeyboardGetActiveView()
  36:     {
  37:         return this.View.FindFirstResponder();
  38:     }
  39:  
  40:     protected virtual void KeyboardWillShowNotification(NSNotification notification)
  41:     {
  42:         UIView activeView = KeyboardGetActiveView();
  43:         if (activeView == null)
  44:             return;
  45:  
  46:         UIScrollView scrollView = activeView.FindSuperviewOfType(this.View, typeof(UIScrollView)) as UIScrollView;
  47:         if (scrollView == null)
  48:             return;
  49:  
  50:         RectangleF keyboardBounds = UIKeyboard.BoundsFromNotification(notification);
  51:  
  52:         UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, keyboardBounds.Size.Height, 0.0f);
  53:         scrollView.ContentInset = contentInsets;
  54:         scrollView.ScrollIndicatorInsets = contentInsets;
  55:  
  56:         RectangleF viewRectAboveKeyboard = new RectangleF(this.View.Frame.Location,
  57:             new SizeF(this.View.Frame.Width, this.View.Frame.Size.Height - keyboardBounds.Size.Height));
  58:  
  59:         RectangleF activeFieldAbsoluteFrame = activeView.Superview.ConvertRectToView(activeView.Frame, this.View);
  60:  
  61:         if (!viewRectAboveKeyboard.Contains(activeFieldAbsoluteFrame))
  62:         {
  63:             PointF scrollPoint = new PointF(0.0f, 
  64:                 activeFieldAbsoluteFrame.Location.Y + activeFieldAbsoluteFrame.Height
  65:                 + scrollView.ContentOffset.Y - viewRectAboveKeyboard.Height);
  66:             scrollView.SetContentOffset(scrollPoint, true);
  67:         }
  68:     }
  69:  
  70:     protected virtual void KeyboardWillHideNotification(NSNotification notification)
  71:     {
  72:         UIView activeView = KeyboardGetActiveView();
  73:         if (activeView == null)
  74:             return;
  75:  
  76:         UIScrollView scrollView = activeView.FindSuperviewOfType(this.View, typeof(UIScrollView)) as UIScrollView;
  77:         if (scrollView == null)
  78:             return;
  79:  
  80:         double animationDuration = UIKeyboard.AnimationDurationFromNotification(notification);
  81:         UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, 0.0f, 0.0f);
  82:         UIView.Animate(animationDuration, delegate
  83:         {
  84:             scrollView.ContentInset = contentInsets;
  85:             scrollView.ScrollIndicatorInsets = contentInsets;
  86:         });
  87:     }
  88: }

2.定义UIView的扩展方法

代码如下:

   1: public static class ViewExtensions
   2: {
   3:     public static UIView FindFirstResponder(this UIView view)
   4:     {
   5:         if (view.IsFirstResponder)
   6:         {
   7:             return view;
   8:         }
   9:         foreach (UIView subView in view.Subviews)
  10:         {
  11:             var firstResponder = subView.FindFirstResponder();
  12:             if (firstResponder != null)
  13:                 return firstResponder;
  14:         }
  15:         return null;
  16:     }
  17:  
  18:     public static UIView FindSuperviewOfType(this UIView view, UIView stopAt, Type type)
  19:     {
  20:         if (view.Superview != null)
  21:         {
  22:             if (type.IsAssignableFrom(view.Superview.GetType()))
  23:             {
  24:                 return view.Superview;
  25:             }
  26:  
  27:             if (view.Superview != stopAt)
  28:                 return view.Superview.FindSuperviewOfType(stopAt, type);
  29:         }
  30:  
  31:         return null;
  32:     }
  33: }

实现的效果:

滚动1滚动2

作者:Bruce Lee
出处:http://www.cnblogs.com/BruceLee521
本博原创文章版权归博客园和本人共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出作者名称和原文连接,否则保留追究法律责任的权利。