wxr
2019-11-25 29a6e6d747d13fc285c8dd86649417f60613388f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Drawing;
using Foundation;
using UIKit;
 
namespace HDL_ON_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;
        }
    }
}