c# - 出现键盘时带有滚动的 ViewController
问题描述
我想看到我所有的视图,但是当出现键盘时我看不到所有视图,例如:
-------------------------------------------
- --------------------------------------- - i want what my viewcontroller have scroll
- - text field 1 - -
- - text field 2 - -
- - text field n - -
- ------------------------------------- - -
-------------------------------------------
- -
- keyboard -
- -
- - - -
- ------------------------------------- - -
这是我的代码,意思是我可以在我的文本字段中修复此代码,我正在使用这个 IsFirstResponder 来查看当前文本字段,但我想查看我的所有文本字段,我可以放一个滚动条吗? :
可以放卷轴吗?我检查了许多应用程序,我认为这是可能的。
public static void KeyBoardUpNotification(NSNotification notification)
{
scrollamount = 0.0f;
RectangleF rectangle = (RectangleF)UIKeyboard.BoundsFromNotification(notification);
bottom = ((float)(activeview.Frame.Y + activeview.Frame.Height + offset));
scrollamount = ((float)(rectangle.Height - (currentView.Frame.Size.Height - bottom)));
if (scrollamount != 0)
{
moveViewUp = true;
ScrollTheView(moveViewUp);
}
else
{
moveViewUp = false;
}
}
public static void KeyBoardDownNotification(NSNotification notification)
{
if (moveViewUp) ScrollTheView(false);
}
private static void ScrollTheView(bool move)
{
UIView.BeginAnimations(string.Empty, IntPtr.Zero);
UIView.SetAnimationDuration(0.3);
RectangleF frame = (RectangleF)currentView.Frame;
if (move)
{
frame.Y = y;
frame.Y -= scrollamount;
}
else
{
frame.Y = y;
scrollamount = 0;
moveViewUp = false;
}
currentView.Frame = frame;
UIView.CommitAnimations();
scrollamount = 0;
frame.Y = 0;
解决方案
您可以使用来自 nuget 的插件Xamarin.IQKeyboardManager。
用法
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
Xamarin.IQKeyboardManager.SharedManager.EnableAutoToolbar = true;
Xamarin.IQKeyboardManager.SharedManager.ShouldResignOnTouchOutside = true;
Xamarin.IQKeyboardManager.SharedManager.ShouldToolbarUsesTextFieldTintColor = true;
Xamarin.IQKeyboardManager.SharedManager.KeyboardDistanceFromTextField = 300f;
//...
}
有关更多详细信息,您可以查看https://github.com/TheEightBot/Xamarin.IQKeyboardManager
推荐阅读
- java - 无法添加两个字节变量值并存储到第三个字节类型变量中
- android - android 不支持写入内部存储
- osrm - 强制包括 OSRM 路线中的所有步骤
- flutter - 有没有办法从 html 包中复制文本
- java - 将具有可序列化的对象传递给另一个片段
- python - 看门狗 - 监视目录以创建新文件,但如果已存在具有相同文件名的文件则忽略
- vb.net - 输入字符串的格式不正确 文本框 vb Visual Basic 2010
- javascript - 让 Nivo Lightbox 在点击时打开多个 img 而不是一个
- mysql - 组合空列以在查询中获得所需的连接
- javascript - 在 onClick 期间切换元素