Created
February 5, 2019 14:09
-
-
Save rdelrosario/137b37e0ea8c0ddf14a001cd0f0284f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UIKit; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.iOS; | |
using Foundation; | |
using CoreGraphics; | |
using KeyboardOptionsSample.iOS; | |
using KeyboardOptionsSample; | |
[assembly: ExportRenderer(typeof(KeyboardView), typeof(KeyboardViewRenderer))] | |
namespace KeyboardOptionsSample.iOS | |
{ | |
public class KeyboardViewRenderer : ViewRenderer | |
{ | |
NSObject _keyboardShowObserver; | |
NSObject _keyboardHideObserver; | |
protected override void OnElementChanged(ElementChangedEventArgs<View> e) | |
{ | |
base.OnElementChanged(e); | |
if (e.NewElement != null) | |
{ | |
RegisterForKeyboardNotifications(); | |
} | |
if (e.OldElement != null) | |
{ | |
UnregisterForKeyboardNotifications(); | |
} | |
} | |
void RegisterForKeyboardNotifications() | |
{ | |
if (_keyboardShowObserver == null) | |
_keyboardShowObserver = UIKeyboard.Notifications.ObserveWillShow(OnKeyboardShow); | |
if (_keyboardHideObserver == null) | |
_keyboardHideObserver = UIKeyboard.Notifications.ObserveWillHide(OnKeyboardHide); | |
} | |
void OnKeyboardShow(object sender, UIKeyboardEventArgs args) | |
{ | |
NSValue result = (NSValue)args.Notification.UserInfo.ObjectForKey(new NSString(UIKeyboard.FrameEndUserInfoKey)); | |
CGSize keyboardSize = result.RectangleFValue.Size; | |
if (Element != null) | |
{ | |
Element.Margin = new Thickness(0, 0, 0, keyboardSize.Height); //push the entry up to keyboard height when keyboard is activated | |
} | |
} | |
void OnKeyboardHide(object sender, UIKeyboardEventArgs args) | |
{ | |
if (Element != null) | |
{ | |
Element.Margin = new Thickness(0); //set the margins to zero when keyboard is dismissed | |
} | |
} | |
void UnregisterForKeyboardNotifications() | |
{ | |
if (_keyboardShowObserver != null) | |
{ | |
_keyboardShowObserver.Dispose(); | |
_keyboardShowObserver = null; | |
} | |
if (_keyboardHideObserver != null) | |
{ | |
_keyboardHideObserver.Dispose(); | |
_keyboardHideObserver = null; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment