【问题标题】:Xamarin solution for: UITableView and keyboard scrolling issueXamarin 解决方案:UITableView 和键盘滚动问题
【发布时间】:2014-03-13 04:57:14
【问题描述】:
我遇到了与UITableView and keyboard scrolling issue 中所述相同的问题,但我使用 MonoTouch/Xamarin.iOS 在 C# 中进行编码。获得一个可行的 C# 解决方案需要一些工作,所以我想我会分享。
我试图解决的问题:我有一个 UIViewController,它包含一个列表视图以及一些其他按钮。这些行本身就是自定义视图,其中包含几个惰性 UILabel 和一个接受输入的 UITextView。如果用户触摸屏幕下半部分的 TextView 之一,键盘将覆盖他们正在编辑的字段。此外,如果它是列表的末尾,您甚至无法手动将其滚动到视图中。
阅读本网站上的帖子后,我得到了很多帮助,我想回馈一些。从几篇文章中总结出来,我认为我有一个(相当)简单、有效的 C# 解决方案。我没有足够的声誉来发表评论,所以我正在创建一个问题并回答,希望其他人可以节省一些时间。请参阅下面的解决方案,如果我遗漏了什么,请告诉我!
【问题讨论】:
标签:
c#
ios
uitableview
xamarin.ios
【解决方案1】:
稍微修改一下页面底部工具栏的高度。此外,一旦识别出“第一响应者”视图,break 语句就会离开 foreach 循环:
void ShowCallback(object sender, UIKit.UIKeyboardEventArgs args)
{
// This happens if the user focuses a textfield outside of the
// tableview when the tableview is empty.
UIView activeView = new UIView();
// Find what opened the keyboard
foreach (UIView view in this.View.Subviews)
{
if (view.IsFirstResponder)
{
activeView = view;
break;
}
}
// Get the size of the keyboard
var keyboardBounds = args.FrameEnd;
// Create an inset and assign it to the tableview
//need to subtract the navbar at the bottom of the scree
nfloat toolbarHeight = 0f;
if (!this.NavigationController.ToolbarHidden)
{
toolbarHeight = this.NavigationController.Toolbar.Frame.Height;
}
nfloat adjustedInset = keyboardBounds.Size.Height - toolbarHeight;
UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, adjustedInset, 0.0f);
ExerciseTableView.ContentInset = contentInsets;
ExerciseTableView.ScrollIndicatorInsets = contentInsets;
// Make sure the tapped location is visible.
ExerciseTableView.ScrollRectToVisible(activeView.Frame, true);
}
【解决方案2】:
这里只是调整键盘的相关代码,当用户在 TextView(和键盘)之外点击时关闭键盘。
我应该注意的是,我的 TableView 位于屏幕的最底部,所以我不必弄清楚它是如何与键盘重叠的。如果您的 TableView 下方有东西,则需要添加一些数学运算。
public partial class myViewController : UIViewController
{
UITapGestureRecognizer _tap;
NSObject _shownotification;
NSObject _hidenotification;
public myViewController() : base("myViewController", null)
{
// This code dismisses the keyboard when the user touches anywhere
// outside the keyboard.
_tap = new UITapGestureRecognizer();
_tap.AddTarget(() =>{
View.EndEditing(true);
});
_tap.CancelsTouchesInView = false;
View.AddGestureRecognizer(_tap);
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
// Register our callbacks
_hidenotification = UIKeyboard.Notifications.ObserveDidHide(HideCallback);
_shownotification = UIKeyboard.Notifications.ObserveWillShow(ShowCallback);
}
public override void ViewWillDisappear(bool animated)
{
// Unregister the callbacks
if (_shownotification != null)
_shownotification.Dispose();
if (_hidenotification != null)
_hidenotification.Dispose();
base.ViewWillDisappear(animated);
}
void ShowCallback (object sender, MonoTouch.UIKit.UIKeyboardEventArgs args)
{
// This happens if the user focuses a textfield outside of the
// tableview when the tableview is empty.
UIView activeView = this.View.FindFirstResponder();
if ((activeView == null) || (activeView == Customer))
return;
// Get the size of the keyboard
RectangleF keyboardBounds = args.FrameEnd;
// Create an inset and assign it to the tableview
UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, keyboardBounds.Size.Height, 0.0f);
myTableView.ContentInset = contentInsets;
myTableView.ScrollIndicatorInsets = contentInsets;
// Make sure the tapped location is visible.
myTableView.ScrollRectToVisible(activeView.Frame, true);
}
void HideCallback (object sender, MonoTouch.UIKit.UIKeyboardEventArgs args)
{
// If the tableView's ContentInset is "zero", we don't need to
// readjust the size
if (myTableView.ContentInset.Top == UIEdgeInsets.Zero.Top)
return;
// Remove the inset when the keyboard is hidden so that the
// TableView will use the whole screen again.
UIView.BeginAnimations (""); {
UIView.SetAnimationCurve (args.AnimationCurve);
UIView.SetAnimationDuration (args.AnimationDuration);
var viewFrame = View.Frame;
var endRelative = View.ConvertRectFromView (args.FrameEnd, null);
viewFrame.Height = endRelative.Y;
View.Frame = viewFrame;
myTableView.ContentInset = UIEdgeInsets.Zero;
myTableView.ScrollIndicatorInsets = UIEdgeInsets.Zero;
} UIView.CommitAnimations ();
}
}
感谢 mickm 发布 Objective-C 解决方案。