ios - Xamarin.form 键盘出现时上移 View

标签 ios xamarin

我正在尝试构建一个聊天应用程序 UI,布局的想法非常简单:

enter image description here

当输入栏获得焦点时,键盘会出现并“插入”聊天栏,因为它是一个网格,ListView 将调整大小以适应屏幕:

enter image description here

我将输入栏的边距更新为“向上推”:

NSValue result = (NSValue)args.Notification.UserInfo.ObjectForKey(new NSString(UIKeyboard.FrameEndUserInfoKey));
CGSize keyboardSize = result.RectangleFValue.Size;
if (Element != null){
Element.Margin = new Thickness(0, 0, 0,keyboardSize.Height); //push the entry up to keyboard height when keyboard is activated
}

这是结果:
https://drive.google.com/file/d/1S9yQ6ks15BRH3hH0j_M8awpDJFRFitUi/view?usp=sharing

View 确实向上推,ListView 也按预期调整了大小,但是有两个问题我不知道如何解决:
  • 调整大小后如何保留 ListView 滚动位置?
  • 缺乏动画来推高 View

  • 我在网上搜索过,尝试了 IQKeyboardManager 和 KeyboardOverLap,俯卧撑动画很好很流畅,但是发生了奇怪的事情:

    https://drive.google.com/file/d/1Zm0lMKB3wq07ve67wlcvLuNM_6Waad7R/view?usp=sharing
  • 这种方法不是调整ListView的大小,而是将整个ListView向上推,我看不到前几项,当然滚动条可以滚动到屏幕外
  • ListView 底部有额外奇怪的空格

  • 任何帮助将不胜感激,谢谢!

    解决方案:
      void OnKeyboardShow(object sender, UIKeyboardEventArgs args)
        {
            NSValue result = (NSValue)args.Notification.UserInfo.ObjectForKey(new NSString(UIKeyboard.FrameEndUserInfoKey));
            CGSize keyboardSize = result.RectangleFValue.Size;
            if (Control != null)
            {
                int bottomMargin = 0;
                var sa = UIApplication.SharedApplication.KeyWindow.SafeAreaInsets;
                bottomMargin = (int)sa.Bottom;
    
                CGPoint offset = Control.ContentOffset;
                var difference = keyboardSize.Height - bottomMargin;
    
                if (Control.ContentSize.Height > Control.Frame.Height)
                {
                    offset.Y += difference;
                    Control.SetContentOffset(offset, true);
                }
                else if (Control.ContentSize.Height + keyboardSize.Height > Control.Frame.Height)
                {
                    offset.Y += Control.ContentSize.Height + keyboardSize.Height - Control.Frame.Height - bottomMargin;
                    Control.SetContentOffset(offset, true);
                }
    
                Control.ContentInset = new UIEdgeInsets(0, 0, difference, 0);
                Control.ScrollIndicatorInsets = Control.ContentInset;
    
            }
    
        }
    
        void OnKeyboardHide(object sender, UIKeyboardEventArgs args)
        {
            if (Control != null)
            {
                Control.ContentInset = new UIEdgeInsets(0, 0, 0, 0);
                Control.ScrollIndicatorInsets = new UIEdgeInsets(0, 0, 0, 0);
            }
    
        }
    

    最佳答案

    解决方案:

    引用以下代码

    in iOS Custom Renderer


    protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
    {
      base.OnElementChanged(e);
    
      if(Control!=null)
      {
         Control.KeyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag;
    
         NSNotificationCenter.DefaultCenter.AddObserver(this, new Selector("KeyBoardWillShow:"), new NSString("UIKeyboardWillShowNotification"), null);
    
         NSNotificationCenter.DefaultCenter.AddObserver(this, new Selector("KeyBoardWillHide:"), new NSString("UIKeyboardWillHideNotification"), null);
    
      }
    
    }
    
    
    [Export("KeyBoardWillShow:")]
    void KeyBoardWillShow(NSNotification note)
    {
      NSValue keyboardRect = (NSValue)note.UserInfo.ObjectForKey(new NSString(UIKeyboard.FrameEndUserInfoKey));
      Control.ContentInset = new UIEdgeInsets(0,0, keyboardRect.RectangleFValue.Size.Height,0);
    }
    
    
    [Export("KeyBoardWillHide:")]
    void KeyBoardWillHide(NSNotification note)
    {
      Control.ContentInset = UIEdgeInsets.Zero;
    } 
    

    关于ios - Xamarin.form 键盘出现时上移 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53467135/

    相关文章:

    ios - Oauth1 错误常量 Swift iO

    ios - 检查 AppDelegate 中 session 中的用户,如果没有则触发 UIViewController

    sql - Xamarin - SQLite.SQLiteException : Constraint using SQLite-net-pcl

    asp.net-web-api - 从 ASP.NET 5 Web API 引用可移植类库 (PCL)

    android-asynctask - Xamarin - 任务等待来自自定义对话框的输入

    Android 在编辑文本字段中输入之前捕获扫描数据

    ios - 接触时从场景中移除 Sprite

    ios - 错误: Stopped workout session cannot be restarted

    android - Firestore离线数据会在后台同步,还是下次启动应用时同步?

    visual-studio - 为 Visual Studio Xamarin 项目推荐的 .gitignore 文件