gpt4 book ai didi

ios - 如何防止键盘覆盖我的 UI 而不是调整它的大小?

转载 作者:技术小花猫 更新时间:2023-10-29 10:20:50 26 4
gpt4 key购买 nike

在 iOS 中,当根节点是 ScrollView 时,Xamarin.Forms 会在键盘出现时调整屏幕大小。但是当根节点不是 ScrollView 时,键盘会隐藏部分 UI。你如何防止这种情况发生?

最佳答案

解决这个问题的方法是使用一个自定义渲染器来监听键盘的显示并在键盘出现时添加填充。

在您的 PCL 项目中,KeyboardResizingAwareContentPage.cs:

using Xamarin.Forms;

public class KeyboardResizingAwareContentPage : ContentPage {
public bool CancelsTouchesInView = true;
}

在您的 iOS 项目中,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);
}
}
}
}

关于ios - 如何防止键盘覆盖我的 UI 而不是调整它的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31172518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com