ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-10-29 23:07:25  阅读:248  来源: 互联网

标签:xamarin-ios ios c 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解决方案.

标签:xamarin-ios,ios,c,uitableview
来源: https://codeday.me/bug/20191029/1963449.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有