如何防止键盘覆盖我的UI而不是调整其大小? [英] How do I keep the keyboard from covering my UI instead of resizing it?
本文介绍了如何防止键盘覆盖我的UI而不是调整其大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在iOS中,当根节点为 ScrollView
时键盘出现时,Xamarin.Forms会调整屏幕大小。但是当根节点不是 ScrollView
时,键盘会隐藏部分UI。你如何防止这种情况发生?
In iOS, Xamarin.Forms resizes the screen when the keyboard comes up when the root node is a ScrollView
. But when the root node is not a ScrollView
the keyboard hides part of the UI. How do you keep this from happening?
推荐答案
修复此问题的方法是使用自定义渲染器监听键盘显示并在其中添加填充。
The way to fix this is with a custom renderer that listens for the keyboard showing up and adds padding while it's there.
在您的PCL项目中, KeyboardResizingAwareContentPage.cs
:
In your PCL project, KeyboardResizingAwareContentPage.cs
:
using Xamarin.Forms;
public class KeyboardResizingAwareContentPage : ContentPage {
public bool CancelsTouchesInView = true;
}
在您的iOS项目中, IosKeyboardFixPageRenderer.cs
:
In your 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 as 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 as ContentPage;
if (page != null && !(page.Content is ScrollView)) {
var padding = page.Padding;
page.Padding = new Thickness(padding.Left, padding.Top, padding.Right, padding.Bottom + frameBegin.Top - frameEnd.Top);
}
}
}
}
这篇关于如何防止键盘覆盖我的UI而不是调整其大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文