它是iOS移动开发中非常常见的问题,当你完成UI并且它包含太多的UITextField时,如果你试图在UITextFields中输入值,那么它们会被添加到屏幕的中心底部;这些字段隐藏在键盘后面。我们怎样才能摆脱这个普遍的问题?
答案 0 :(得分:1)
当键盘可见并隐藏时,您可以在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();
}
答案 1 :(得分:-1)
我使用nuget
包解决了这个问题。我已经覆盖了两个方法并在这些方法中初始化了代码。
下载KeyboardHandler并使用以下内容:
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();
}