Xamarin解决方案:UITableView和键盘滚动问题

时间:2014-02-14 01:14:55

标签: c# ios uitableview xamarin.ios

我遇到的问题与UITableView and keyboard scrolling issue中描述的相同,但我使用MonoTouch / Xamarin.iOS编写C#。获得一个有效的C#解决方案需要花一些时间,所以我想我会分享。

我试图解决的问题:我有一个UIViewController,它包含一个列表视图以及一些其他按钮。行本身是自定义视图,包含几个惰性UILabel和一个接受输入的UITextView。如果用户触摸屏幕下半部分中的一个TextView,键盘将覆盖他们正在编辑的字段。此外,如果它是列表的末尾,您甚至无法手动将其滚动到视图中。

通过阅读本网站上的帖子,我得到了很多帮助,我想回馈一些。从几个帖子中拉出比特,我想我有一个(合理的)简单,有效的C#解决方案。我没有足够的声誉来发表评论,所以我正在创建一个问题并回答希望其他人可以节省一些时间。请参阅下面的解决方案,如果我遗漏了某些内容,请告诉我!

2 个答案:

答案 0 :(得分:4)

以下是调整键盘的相关代码,并在用户点击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解决方案。

答案 1 :(得分:0)

略微修改会考虑页面底部工具栏的高度。此外,一个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);
        }