gpt4 book ai didi

ios - 键盘在 UIViewController Xamarin IOS 中隐藏 UITextView

转载 作者:行者123 更新时间:2023-12-02 10:45:12 24 4
gpt4 key购买 nike

我创建了一个 UIViewController 并在 UIViewController 上放置了一个标签和 UITextView

我想将一些 UITextView 放在我的 UIViewController 上,一个放在另一个下面。我仅在我的应用程序中使用横向模式。我正在使用 Xamarin IOS 来制作这个应用程序。

下面的屏幕显示了正在发生的事情!有人可以帮助我吗!

Showing UITextView.

Keyboard is hiding my textview field.

最佳答案

您需要在发生此问题的 View Controller 中添加观察者,如下所示。

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 方法。本质上,您计算控件是否会被键盘隐藏,如果是,则计算 View 需要移动多少才能显示控件,然后移动它。

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/

关于ios - 键盘在 UIViewController Xamarin IOS 中隐藏 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41128016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com