【发布时间】:2014-08-29 08:44:22
【问题描述】:
我正在实现一个在某种程度上类似于 Messages 应用程序中发生的视图,因此有一个 UITextView 附加到屏幕底部的视图,还有一个 UITableView 显示主要内容。当它被点击时它会随着键盘向上滑动,当键盘被关闭时它会滑回屏幕底部。
我拥有的那部分工作正常 - 我刚刚订阅了键盘通知 - 将隐藏并显示。
问题是我已将 UITableView 上的键盘关闭模式设置为交互,并且在平移时我无法捕获对键盘的更改。
第二个问题是这个带有uitextview的栏覆盖了uitableview的某些部分。如何解决这个问题?我仍然希望 uitableview 在此栏“下方”,就像在消息应用中一样。
我在所有地方都使用 AutoLayout。
任何帮助将不胜感激!
============
编辑1: 这是一些代码:
View Hierarchy如下:
查看 - UITableView(这个将包含“消息”) - UIView(这个会滑动)
UITableView 对父视图的顶部、左侧、右侧和底部有限制,因此它会填满整个屏幕。 UIView 对父视图的左、右和底部有约束,因此它粘在底部 - 我通过调整约束上的常量来移动它。
在 ViewWillAppear 方法中:
NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.DidShowNotification, OnKeyboardDidShowNotification);
NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.WillChangeFrameNotification, OnKeyboardDidShowNotification);
NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.WillHideNotification, OnKeyboardWillHideNotification);
这里有一些方法:
void OnKeyboardDidShowNotification (NSNotification notification)
{
AdjustViewToKeyboard (Ui.KeyboardHeightFromNotification (notification), notification);
}
void OnKeyboardWillHideNotification (NSNotification notification)
{
AdjustViewToKeyboard (0.0f, notification);
}
void AdjustViewToKeyboard (float offset, NSNotification notification = null)
{
commentEditViewBottomConstraint.Constant = -offset;
if (notification != null) {
UIView.BeginAnimations (null, IntPtr.Zero);
UIView.SetAnimationDuration (Ui.KeyboardAnimationDurationFromNotification (notification));
UIView.SetAnimationCurve ((UIViewAnimationCurve)Ui.KeyboardAnimationCurveFromNotification (notification));
UIView.SetAnimationBeginsFromCurrentState (true);
}
View.LayoutIfNeeded ();
commentEditView.LayoutIfNeeded ();
var insets = commentsListView.ContentInset;
insets.Bottom = offset;
commentsListView.ContentInset = insets;
if (notification != null) {
UIView.CommitAnimations ();
}
}
【问题讨论】:
-
键盘出现时你如何移动文本视图。你能显示一些代码。不是 100% 确定你在问什么。
-
已添加代码,希望对您有所帮助。第一个问题是 UITableView 将 KeyboardDismissMode 设置为 Interactive,因此当用户向下滑动手指时,键盘被关闭 - 不是立即,而是“跟随”手指。我不知道如何更好地解释它 - 尝试在消息应用程序中进行。在这个实现中,键盘正确地“跟随”手指,但我的 UIView 没有。
-
第二个问题是,因为我的 UIView 位于屏幕底部,它覆盖了 UITableView 的某些部分,并且它没有正确调整内容插入 - 我需要以某种方式调整它们。
标签: ios uitableview xamarin autolayout uikeyboard