【发布时间】:2017-04-03 22:40:07
【问题描述】:
这是 iOS 移动开发中非常常见的问题,即当您完成 UI 并且它包含太多 UITextFields 时,如果您尝试在 UITextFields 中输入值,这些值会添加到屏幕的中心底部;这些字段隐藏在键盘后面。我们怎样才能摆脱这个普遍的问题?
【问题讨论】:
-
伙计们,请说明您为什么拒绝投票,以便我可以改进和改进。谢谢。
标签: xamarin xamarin.ios
这是 iOS 移动开发中非常常见的问题,即当您完成 UI 并且它包含太多 UITextFields 时,如果您尝试在 UITextFields 中输入值,这些值会添加到屏幕的中心底部;这些字段隐藏在键盘后面。我们怎样才能摆脱这个普遍的问题?
【问题讨论】:
标签: xamarin xamarin.ios
您可以在 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();
}
【讨论】:
我使用了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();
}
【讨论】: