我有一个表,在这个表下的单元格 y 中有一个 textField 我有一个 textField
-------------------------------------------
- --------------------------------------- -
- - text field in table - -
- ------------------------------------- - -
-------------------------------------------
- --------------------------------------- -
- - other textField - -
- ------------------------------------- - -
然后我为键盘入口做了一个库
public static void KeyBoardUpNotification(NSNotification notification)
{
scrollamount = 0.0f;
RectangleF rectangle = (RectangleF)UIKeyboard.BoundsFromNotification(notification);
if (currentView == null)
{
return;
}
if (activeview==null) {
foreach (UIView view in currentView.Subviews)
{
if (view.IsFirstResponder)
{
activeview = view;
activeview.BecomeFirstResponder();
}
}
}
if (activeview == null)
{
return;
}
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;
}
但是当焦点在单元格的文本字段中时,此代码不起作用,相机会聚焦在下面的文本字段上。
但此代码仅适用于下方的文本字段,相机会聚焦在下方的文本字段上,这很好。
最佳答案
可以在textfield下方
添加标签,与activeview
比较,再决定是上移textField下方
还是 tableView
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
UITextField belowTextField = new UITextField();
belowTextField.Tag = 1;
}
public void test() {
if (activeview == null)
{
foreach (UIView view in currentView.Subviews)
{
if (view.IsFirstResponder)
{
activeview = view;
activeview.BecomeFirstResponder();
}
}
}
UITextField tempView = activeview as UITextField;
if (tempView.Tag == 1)
{
//move up bellowTextField
}
else {
//move up tableView
}
}
关于c# - 键盘出现时如何修复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59703833/