From f1c3921b08bb22ac6f5db22d620e01d7e8e5c49f Mon Sep 17 00:00:00 2001
From: WJC <wjc@hdlchina.com.cn>
Date: 星期一, 30 十二月 2019 13:32:33 +0800
Subject: [PATCH] 2019-12-30-1

---
 ZigbeeApp/GateWay.Ios/ViewControllerBase.cs |  137 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 137 insertions(+), 0 deletions(-)

diff --git a/ZigbeeApp/GateWay.Ios/ViewControllerBase.cs b/ZigbeeApp/GateWay.Ios/ViewControllerBase.cs
new file mode 100755
index 0000000..1fa9122
--- /dev/null
+++ b/ZigbeeApp/GateWay.Ios/ViewControllerBase.cs
@@ -0,0 +1,137 @@
+锘縰sing System;
+using System.Drawing;
+using Foundation;
+using UIKit;
+
+namespace GateWay.Ios
+{
+    public class ViewControllerBase : Shared.BaseViewController
+    {
+        public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
+        {
+            return false;
+        }
+
+        NSObject _keyboardObserverWillShow;
+        NSObject _keyboardObserverWillHide;
+ 
+        public override bool ShouldAutorotate ()
+        {
+            return false;
+        }
+
+        public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations ()
+        {
+            if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad) {
+                UIApplication.SharedApplication.SetStatusBarOrientation (UIInterfaceOrientation.LandscapeRight, false);
+                return UIInterfaceOrientationMask.LandscapeRight;
+            } else {
+                return UIInterfaceOrientationMask.Portrait;
+            }
+        }
+
+        public override void ViewDidLoad ()
+        {
+            base.ViewDidLoad ();
+            //璁剧疆閿洏浜嬩欢澶勭悊绋嬪簭
+            RegisterForKeyboardNotifications ();
+        }
+        protected virtual void RegisterForKeyboardNotifications ()
+        {
+            _keyboardObserverWillShow = NSNotificationCenter.DefaultCenter.AddObserver
+                (UIKeyboard.WillShowNotification, KeyboardWillShowNotification);
+            _keyboardObserverWillHide = NSNotificationCenter.DefaultCenter.AddObserver
+                (UIKeyboard.WillHideNotification, KeyboardWillHideNotification);
+        }
+        protected virtual void UnregisterKeyboardNotifications ()
+        {
+            NSNotificationCenter.DefaultCenter.RemoveObserver (_keyboardObserverWillShow);
+            NSNotificationCenter.DefaultCenter.RemoveObserver (_keyboardObserverWillHide);
+        }
+        protected virtual UIView KeyboardGetActiveView ()
+        {
+            return this.View.FindFirstResponder ();
+        }
+        protected virtual void KeyboardWillShowNotification (NSNotification notification)
+        {
+            UIView activeView = KeyboardGetActiveView ();
+            if (activeView == null)
+                return;
+            UIView scrollView = activeView.FindSuperviewOfType_Self (this.View, typeof (UIView)) as UIView;
+            if (scrollView == null)
+                return;
+            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)) {
+                //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,
+                                                             (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)
+        {
+            UIView activeView = KeyboardGetActiveView ();
+            if (activeView == null)
+                return;
+            UIView scrollView = activeView.FindSuperviewOfType_Self (this.View, typeof (UIView)) as UIView;
+            if (scrollView == null)
+                return;
+            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);
+            
+        }
+    }
+
+    public static class ViewExtensions
+    {
+        public static UIView FindFirstResponder (this UIView view)
+        {
+            if (view.IsFirstResponder) {
+                return view;
+            }
+            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)
+        {
+            if (view.Superview != null) {
+                if (type.IsAssignableFrom (view.Superview.GetType ())) {
+                    return view.Superview;
+                }
+                if (view.Superview != stopAt)
+                    return view.Superview.FindSuperviewOfType (stopAt, type);
+            }
+            return null;
+        }
+
+        public static UIView FindSuperviewOfType_Self (this UIView view, UIView stopAt, Type type)
+        {
+            if (view.Superview != null) {
+                return view.Superview.FindSuperviewOfType_Self (stopAt, type);
+            }
+            return view;
+        }
+    }
+}

--
Gitblit v1.8.0