| | |
| | | using System.Drawing; |
| | | using Foundation; |
| | | using UIKit; |
| | | |
| | | using WebKit; |
| | | namespace HDL_ON_iOS |
| | | { |
| | | public class ViewControllerBase : Shared.BaseViewController |
| | |
| | | // } |
| | | //} |
| | | |
| | | public override void ViewDidLoad () |
| | | public override void ViewDidLoad() |
| | | { |
| | | base.ViewDidLoad (); |
| | | base.ViewDidLoad(); |
| | | //设置键盘事件处理程序 |
| | | RegisterForKeyboardNotifications (); |
| | | RegisterForKeyboardNotifications(); |
| | | } |
| | | protected virtual void RegisterForKeyboardNotifications () |
| | | protected virtual void RegisterForKeyboardNotifications() |
| | | { |
| | | _keyboardObserverWillShow = NSNotificationCenter.DefaultCenter.AddObserver |
| | | (UIKeyboard.WillShowNotification, KeyboardWillShowNotification); |
| | | _keyboardObserverWillHide = NSNotificationCenter.DefaultCenter.AddObserver |
| | | (UIKeyboard.WillHideNotification, KeyboardWillHideNotification); |
| | | } |
| | | protected virtual void UnregisterKeyboardNotifications () |
| | | protected virtual void UnregisterKeyboardNotifications() |
| | | { |
| | | NSNotificationCenter.DefaultCenter.RemoveObserver (_keyboardObserverWillShow); |
| | | NSNotificationCenter.DefaultCenter.RemoveObserver (_keyboardObserverWillHide); |
| | | NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardObserverWillShow); |
| | | NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardObserverWillHide); |
| | | } |
| | | protected virtual UIView KeyboardGetActiveView () |
| | | protected virtual UIView KeyboardGetActiveView() |
| | | { |
| | | return this.View.FindFirstResponder (); |
| | | return this.View.FindFirstResponder(); |
| | | } |
| | | protected virtual void KeyboardWillShowNotification (NSNotification notification) |
| | | protected virtual void KeyboardWillShowNotification(NSNotification notification) |
| | | { |
| | | UIView activeView = KeyboardGetActiveView (); |
| | | UIView activeView = KeyboardGetActiveView(); |
| | | if (activeView == null) |
| | | return; |
| | | UIView scrollView = activeView.FindSuperviewOfType_Self (this.View, typeof (UIView)) as UIView; |
| | | string sss = activeView.ToString(); |
| | | if (sss.StartsWith("<WKContentView")) |
| | | return; |
| | | UIView scrollView = activeView.FindSuperviewOfType_Self(this.View, typeof(UIView)) as UIView; |
| | | if (scrollView == null) |
| | | return; |
| | | CoreGraphics.CGRect keyboardBounds = UIKeyboard.BoundsFromNotification (notification); |
| | | CoreGraphics.CGRect keyboardBounds = UIKeyboard.BoundsFromNotification(notification); |
| | | //UIEdgeInsets contentInsets = new UIEdgeInsets (0.0f, 0.0f, keyboardBounds.Size.Height, 0.0f); |
| | | //scrollView.ContentInset = contentInsets; |
| | | //scrollView.ScrollIndicatorInsets = contentInsets; |
| | | CoreGraphics.CGRect viewRectAboveKeyboard = new CoreGraphics.CGRect (this.View.Frame.Location, |
| | | new CoreGraphics.CGSize (this.View.Frame.Width, this.View.Frame.Size.Height - keyboardBounds.Size.Height)); |
| | | CoreGraphics.CGRect activeFieldAbsoluteFrame = activeView.Superview.ConvertRectToView (activeView.Frame, this.View); |
| | | if (!viewRectAboveKeyboard.Contains (activeFieldAbsoluteFrame)) { |
| | | CoreGraphics.CGRect viewRectAboveKeyboard = new CoreGraphics.CGRect(this.View.Frame.Location, |
| | | new CoreGraphics.CGSize(this.View.Frame.Width, this.View.Frame.Size.Height - keyboardBounds.Size.Height)); |
| | | CoreGraphics.CGRect activeFieldAbsoluteFrame = activeView.Superview.ConvertRectToView(activeView.Frame, this.View); |
| | | if (!viewRectAboveKeyboard.Contains(activeFieldAbsoluteFrame)) |
| | | { |
| | | //PointF scrollPoint = new PointF (0.0f, |
| | | // (float)(activeFieldAbsoluteFrame.Location.Y + activeFieldAbsoluteFrame.Height |
| | | // + scrollView.ContentOffset.Y - viewRectAboveKeyboard.Height)); |
| | | // scrollView.SetContentOffset (scrollPoint, true); |
| | | |
| | | scrollView.Bounds = new CoreGraphics.CGRect (0.0f, |
| | | scrollView.Bounds = new CoreGraphics.CGRect(0.0f, |
| | | (float)(activeFieldAbsoluteFrame.Location.Y + activeFieldAbsoluteFrame.Height |
| | | + scrollView.Frame.Y - viewRectAboveKeyboard.Height) |
| | | , this.View.Frame.Width, this.View.Frame.Height); |
| | |
| | | //if (this.View.Frame.Height - activeView.Frame.Bottom < keyboardBounds.Size.Height) |
| | | // scrollView.Bounds = new CoreGraphics.CGRect (0.0f, this.View.Frame.Height - activeView.Frame.Height - keyboardBounds.Size.Height, this.View.Frame.Width, this.View.Frame.Height); |
| | | } |
| | | protected virtual void KeyboardWillHideNotification (NSNotification notification) |
| | | protected virtual void KeyboardWillHideNotification(NSNotification notification) |
| | | { |
| | | UIView activeView = KeyboardGetActiveView (); |
| | | UIView activeView = KeyboardGetActiveView(); |
| | | if (activeView == null) |
| | | return; |
| | | UIView scrollView = activeView.FindSuperviewOfType_Self (this.View, typeof (UIView)) as UIView; |
| | | string sss = activeView.ToString(); |
| | | if (sss.StartsWith("<WKContentView")) |
| | | return; |
| | | UIView scrollView = activeView.FindSuperviewOfType_Self(this.View, typeof(UIView)) as UIView; |
| | | if (scrollView == null) |
| | | return; |
| | | double animationDuration = UIKeyboard.AnimationDurationFromNotification (notification); |
| | | double animationDuration = UIKeyboard.AnimationDurationFromNotification(notification); |
| | | //UIEdgeInsets contentInsets = new UIEdgeInsets (0.0f, 0.0f, 0.0f, 0.0f); |
| | | //UIView.Animate (animationDuration, delegate { |
| | | // scrollView.ContentInset = contentInsets; |
| | | // scrollView.ScrollIndicatorInsets = contentInsets; |
| | | //}); |
| | | scrollView.Bounds = new CoreGraphics.CGRect (0.0f, 0.0f, this.View.Frame.Width, this.View.Frame.Height); |
| | | |
| | | scrollView.Bounds = new CoreGraphics.CGRect(0.0f, 0.0f, this.View.Frame.Width, this.View.Frame.Height); |
| | | |
| | | } |
| | | } |
| | | |
| | | public static class ViewExtensions |
| | | { |
| | | public static UIView FindFirstResponder (this UIView view) |
| | | public static UIView FindFirstResponder(this UIView view) |
| | | { |
| | | if (view.IsFirstResponder) { |
| | | if (view.IsFirstResponder) |
| | | { |
| | | return view; |
| | | } |
| | | foreach (UIView subView in view.Subviews) { |
| | | var firstResponder = subView.FindFirstResponder (); |
| | | foreach (UIView subView in view.Subviews) |
| | | { |
| | | var firstResponder = subView.FindFirstResponder(); |
| | | if (firstResponder != null) |
| | | return firstResponder; |
| | | } |
| | | return null; |
| | | } |
| | | public static UIView FindSuperviewOfType (this UIView view, UIView stopAt, Type type) |
| | | public static UIView FindSuperviewOfType(this UIView view, UIView stopAt, Type type) |
| | | { |
| | | if (view.Superview != null) { |
| | | if (type.IsAssignableFrom (view.Superview.GetType ())) { |
| | | if (view.Superview != null) |
| | | { |
| | | if (type.IsAssignableFrom(view.Superview.GetType())) |
| | | { |
| | | return view.Superview; |
| | | } |
| | | if (view.Superview != stopAt) |
| | | return view.Superview.FindSuperviewOfType (stopAt, type); |
| | | return view.Superview.FindSuperviewOfType(stopAt, type); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static UIView FindSuperviewOfType_Self (this UIView view, UIView stopAt, Type type) |
| | | public static UIView FindSuperviewOfType_Self(this UIView view, UIView stopAt, Type type) |
| | | { |
| | | if (view.Superview != null) { |
| | | return view.Superview.FindSuperviewOfType_Self (stopAt, type); |
| | | if (view.Superview != null) |
| | | { |
| | | return view.Superview.FindSuperviewOfType_Self(stopAt, type); |
| | | } |
| | | return view; |
| | | } |