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;
|
}
|
}
|
}
|