ios - How do I keep the keyboard from covering my UI instead of resizing it? -
in ios, xamarin.forms resizes screen when keyboard comes when root node scrollview
. when root node not scrollview
keyboard hides part of ui. how keep happening?
the way fix custom renderer listens keyboard showing , adds padding while it's there.
in pcl project, keyboardresizingawarecontentpage.cs
:
using xamarin.forms; public class keyboardresizingawarecontentpage : contentpage { public bool cancelstouchesinview = true; }
in ios project, ioskeyboardfixpagerenderer.cs
:
using foundation; using myproject.ios.renderers; using uikit; using xamarin.forms; using xamarin.forms.platform.ios; [assembly: exportrenderer(typeof(keyboardresizingawarecontentpage), typeof(ioskeyboardfixpagerenderer))] namespace myproject.ios.renderers { public class ioskeyboardfixpagerenderer : pagerenderer { nsobject observerhidekeyboard; nsobject observershowkeyboard; public override void viewdidload() { base.viewdidload(); var cp = element keyboardresizingawarecontentpage; if (cp != null && !cp.cancelstouchesinview) { foreach (var g in view.gesturerecognizers) { g.cancelstouchesinview = false; } } } public override void viewwillappear(bool animated) { base.viewwillappear(animated); observerhidekeyboard = nsnotificationcenter.defaultcenter.addobserver(uikeyboard.willhidenotification, onkeyboardnotification); observershowkeyboard = nsnotificationcenter.defaultcenter.addobserver(uikeyboard.willshownotification, onkeyboardnotification); } public override void viewwilldisappear(bool animated) { base.viewwilldisappear(animated); nsnotificationcenter.defaultcenter.removeobserver(observerhidekeyboard); nsnotificationcenter.defaultcenter.removeobserver(observershowkeyboard); } void onkeyboardnotification(nsnotification notification) { if (!isviewloaded) return; var framebegin = uikeyboard.framebeginfromnotification(notification); var frameend = uikeyboard.frameendfromnotification(notification); var page = element contentpage; if (page != null && !(page.content scrollview)) { var padding = page.padding; page.padding = new thickness(padding.left, padding.top, padding.right, padding.bottom + framebegin.top - frameend.top); } } } }
Comments
Post a Comment