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

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

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

阅读本网站上的帖子后,我得到了很多帮助,我想回馈一下.通过从几篇文章中汇总,我认为我有一个(合理的)简单,有效的C#解决方案.我没有足够的声誉来发表评论,所以我创建了一个问题并回答,希望其他人可以节省一些时间.请参阅下面的解决方案,如果我缺少任何东西,请告诉我!

解决方法:

这里只是调整键盘的相关代码,当用户在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解决方案.

上一篇:IOS设计模式之二(门面模式,装饰器模式)


下一篇:关于STM32的IAP与APP互相跳转