在Xamarin iOS中键入时如何处理UITextField位置?
这是iOS移动开发中非常常见的问题,即当您使用UI时,它包含太多UITextFields,如果您尝试在UITextFields中输入值,这些值将添加到屏幕的中间底部;这些字段隐藏在键盘后面。我们怎样才能摆脱这个普遍的问题呢?我使用了一个在Xamarin iOS中键入时如何处理UITextField位置?,xamarin,xamarin.ios,Xamarin,Xamarin.ios,这是iOS移动开发中非常常见的问题,即当您使用UI时,它包含太多UITextFields,如果您尝试在UITextFields中输入值,这些值将添加到屏幕的中间底部;这些字段隐藏在键盘后面。我们怎样才能摆脱这个普遍的问题呢?我使用了一个nuget包来解决这个问题。我覆盖了两个方法,并在这些方法中初始化了代码 下载并按以下方式使用: using KeyboardHandler; public override void ViewWillAppear(bool animated) { bas
nuget
包来解决这个问题。我覆盖了两个方法,并在这些方法中初始化了代码
下载并按以下方式使用:
using KeyboardHandler;
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
this.yourScrollView.SubscribeKeyboardManaqger();
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
this.yourScrollView.UnsubscribeKeyboardManaqger();
}
您可以使用NSNotificationCenter中的AddObserver方法来确定键盘的可见和隐藏时间 示例代码(仅供参考:去年某个时候从另一篇文章中获得了下面的代码,我记不起该文章的链接,但效果很好。) 在viewdidload方法中调用AddObserver //键盘弹出窗口NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.DidShowNotification,KeyBoardUpNotification) //键盘关闭NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification,KeyBoardDownNotification) 如果有以下方法,可以在基本控制器中添加这些方法
public void KeyBoardUpNotification(NSNotification notification) {
CGRect keyboardSize = UIKeyboard.BoundsFromNotification(notification);
// Find what opened the keyboard
foreach (UIView view in this.View.Subviews) {
if (view.IsFirstResponder)
activeview = view;
}
bottom = (activeview.Frame.Y + activeview.Frame.Height + offset);
scrollamount = (keyboardSize.Height - (View.Frame.Size.Height - bottom));
if (scrollamount > 0) {
moveViewUp = true;
MoveView(moveViewUp);
} else {
moveViewUp = false;
}
}
public void KeyBoardDownNotification(NSNotification notification) {
if (moveViewUp) {
MoveView(false);
}
}
private void MoveView(bool move) {
UIView.BeginAnimations(string.Empty, IntPtr.Zero);
UIView.SetAnimationDuration(0.3);
CGRect frame = View.Frame;
if (move) {
frame.Y -= scrollamount;
} else {
frame.Y += scrollamount;
scrollamount = 0;
}
View.Frame = frame;
UIView.CommitAnimations();
}
谢谢@megaKertz。我为什么要用它?而我可以用两行代码来完成!仅供参考,这不是一种空闲的方法,它有很多漏洞,在键入时也会产生意想不到的结果@RIYAZ从未说过你应该使用它,如果你认为它能解决你的问题,你可以。你用的两行代码是什么?我不是这个意思。我使用了它,发现代码中有很多问题,ScrollView的行为很奇怪,所以我删除了它。伙计们,请说明你们为什么否决它,以便我可以改进和改进它。非常感谢。