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

C# Xamarin解决方案:UITableView和键盘滚动问题,c#,ios,uitableview,xamarin.ios,C#,Ios,Uitableview,Xamarin.ios,我遇到了与中所述相同的问题,但我正在使用MonoTouch/Xamarin.iOS用C#编写代码。要得到一个有效的C#解决方案需要做一点工作,所以我想我会与大家分享 我试图解决的问题是:我有一个UIViewController,它包含一个列表视图以及一些其他按钮。这些行本身是自定义视图,包含多个惰性UILabel和一个接受输入的UITextView。如果用户触摸屏幕下半部分的一个文本视图,键盘将覆盖他们正在编辑的字段。此外,如果它是列表的末尾,您甚至无法手动将其滚动到视图中 我从这个网站上的帖子

我遇到了与中所述相同的问题,但我正在使用MonoTouch/Xamarin.iOS用C#编写代码。要得到一个有效的C#解决方案需要做一点工作,所以我想我会与大家分享

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


我从这个网站上的帖子中得到了很多帮助,我想回馈一些。从几篇文章中收集到的信息来看,我认为我有一个(合理地)简单、有效的C#解决方案。我没有足够的声誉来发表评论,所以我创建了一个问题和回答,希望其他人可以节省一些时间。下面是我的解决方案,如果我遗漏了什么,请告诉我

以下是调整键盘的相关代码,当用户点击文本视图(和键盘)外部时,关闭键盘

我应该注意的是,需要注意的是,我的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 ();
    }
}

感谢您发布Objective-C解决方案。

稍微修改一下页面底部工具栏的高度。此外,一旦识别出“first responder”视图,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);
        }