键盘在UIViewController Xamarin IOS中隐藏了UITextView
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了键盘在UIViewController Xamarin IOS中隐藏了UITextView相关的知识,希望对你有一定的参考价值。
我在UIViewController
上创建了一个UITextView
并放置了一个标签和UIViewController
。
我想把一些UITextView
s放在我的UIViewController
上。我只在我的应用程序中使用横向模式。我正在使用Xamarin ios来制作这个应用程序。
屏幕下方显示正在发生的事情!有人可以帮帮我吗!
答案
您需要在viewcontroller中添加观察者,如下所示出现此问题。
键盘观察员为ViewDidLoad()
。
// Keyboard popup
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.DidShowNotification,KeyBoardUpNotification);
// Keyboard Down
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.WillHideNotification,KeyBoardDownNotification);
// Keyboard popup
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.DidShowNotification,KeyBoardUpNotification);
// Keyboard Down
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.WillHideNotification,KeyBoardDownNotification);
首先是KeyboardUpNotification
方法。基本上你计算控件是否会被键盘隐藏,如果是这样,计算需要移动多少视图来显示控件,然后移动它。
private void KeyBoardUpNotification(NSNotification notification)
{
// get the keyboard size
RectangleF r = UIKeyboard.BoundsFromNotification (notification);
// Find what opened the keyboard
foreach (UIView view in this.View.Subviews) {
if (view.IsFirstResponder)
activeview = view;
}
// Bottom of the controller = initial position + height + offset
bottom = (activeview.Frame.Y + activeview.Frame.Height + offset);
// Calculate how far we need to scroll
scroll_amount = (r.Height - (View.Frame.Size.Height - bottom)) ;
// Perform the scrolling
if (scroll_amount > 0) {
moveViewUp = true;
ScrollTheView (moveViewUp);
} else {
moveViewUp = false;
}
}
private void KeyBoardUpNotification(NSNotification notification)
{
// get the keyboard size
RectangleF r = UIKeyboard.BoundsFromNotification (notification);
// Find what opened the keyboard
foreach (UIView view in this.View.Subviews) {
if (view.IsFirstResponder)
activeview = view;
}
// Bottom of the controller = initial position + height + offset
bottom = (activeview.Frame.Y + activeview.Frame.Height + offset);
// Calculate how far we need to scroll
scroll_amount = (r.Height - (View.Frame.Size.Height - bottom)) ;
// Perform the scrolling
if (scroll_amount > 0) {
moveViewUp = true;
ScrollTheView (moveViewUp);
} else {
moveViewUp = false;
}
}
活动字段用于跟踪当前启动的文本字段。
public override void EditingStarted (UITextField textField)
{
activeview = textField;
}
更多:http://www.gooorack.com/2013/08/28/xamarin-moving-the-view-on-keyboard-show/
以上是关于键盘在UIViewController Xamarin IOS中隐藏了UITextView的主要内容,如果未能解决你的问题,请参考以下文章
当使用 Swift 出现键盘时如何向上移动 UIViewController 的内容
关闭在 UINavigationController 堆栈中的 UIViewController 的键盘
键盘在UIViewController Xamarin IOS中隐藏了UITextView