Scroll editor in Xamarin Forms into view

前端 未结 6 1326
天涯浪人
天涯浪人 2021-02-02 01:49

Using Xamarin Forms, consider the Xaml below.


   

        
6条回答
  •  眼角桃花
    2021-02-02 02:05

    Sometimes you cannot put your main view in a scrollview, in those cases you can implement this by handling the keyboard events the iOS project and passing them to the Forms level. Android takes care of it's self.

    using System;
    using Foundation;
    using UIKit;
    using RaiseKeyboard.iOS;
    
    [assembly: Xamarin.Forms.Dependency (typeof (KeyboardHelper))]
    namespace RaiseKeyboard.iOS
    {
        // Raises keyboard changed events containing the keyboard height and
        // whether the keyboard is becoming visible or not
        public class KeyboardHelper : IKeyboardHelper
        {
            public KeyboardHelper() {
                NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, OnKeyboardNotification);
                NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, OnKeyboardNotification);
            }
    
            public event EventHandler KeyboardChanged;
    
            private void OnKeyboardNotification (NSNotification notification)
            {
                var visible = notification.Name == UIKeyboard.WillShowNotification;
                var keyboardFrame = visible
                    ? UIKeyboard.FrameEndFromNotification(notification)
                    : UIKeyboard.FrameBeginFromNotification(notification);
                if (KeyboardChanged != null) {
                    KeyboardChanged (this, new KeyboardHelperEventArgs (visible, (float)keyboardFrame.Height));
                }
            }
        }
    }
    

    Then at the Forms level:

    using System;
    using Xamarin.Forms;
    
    namespace RaiseKeyboard
    {
        // Provides static access to keyboard events
        public static class KeyboardHelper
        {
            private static IKeyboardHelper keyboardHelper = null;
    
            public static void Init() {
                if (keyboardHelper == null) {
                    keyboardHelper = DependencyService.Get();
                }
            }
    
            public static event EventHandler KeyboardChanged {
                add {
                    Init();
                    keyboardHelper.KeyboardChanged += value;
                }
                remove {
                    Init ();
                    keyboardHelper.KeyboardChanged -= value;
                }
            }
        }
    
        public interface IKeyboardHelper
        {
            event EventHandler KeyboardChanged;
        }
    
        public class KeyboardHelperEventArgs : EventArgs 
        {
            public readonly bool Visible;
            public readonly float Height;
    
            public KeyboardHelperEventArgs(bool visible, float height) {
                Visible = visible;
                Height = height;
            }
        }
    
    }
    

    If you are working in a Stacklayout and wish to raise the view above the keyboard you can put a spacer with a height 0 at the bottom of the stack. Then set it to the height of the keyboard when the keyboard changed event is raised.

    spacer.HeightRequest = e.Visible ? e.Height : 0;
    

    If you are working with a Listview you can handle this by translating your view by the amount it would have been overlapped by.

    bottomOffset = mainStack.Bounds.Bottom - textStack.Bounds.Bottom;
    textStack.TranslationY -= e.Visible ? e.Height - bottomOffset : bottomOffset - e.Height;
    

    Listviews have to be handled differently as there height is automatically adjusted by Forms and using a spacer results over correction.

    Sample here: https://github.com/naturalistic/raisekeyboard

提交回复
热议问题