scroll the view in ios like android.

bprasadbprasad USMember ✭✭

hi all am new to xamarin native ios...please help me to get stuckout.
i have registration form(bunch of text box and a button) and i need that to scroll when keyboard up .i have used lots of example but failed to get the result.
[[structure of my UIViewContoller is UIViewController-->UIview-->UIScrollView -->UIView-->controls...]]
please refer a link or recipe answer for my problem

Tagged:

Best Answer

Answers

  • bprasadbprasad USMember ✭✭

    yes i have tried this also but get lots of errors
    i have tried this code

    using System;
    using UIKit;
    using CoreGraphics;
    using Foundation;
    using System.Drawing;

    namespace myapp.iOS
    {
    public partial class testController : UIViewController
    {
    public testController (IntPtr handle) : base (handle)
    {
    }

        public override void ViewDidLoad ()
        {
            // Perform any additional setup after loading the view, typically from a nib.
            var g = new UITapGestureRecognizer (() => View.EndEditing (true));
            g.CancelsTouchesInView = false; //for iOS5
            View.AddGestureRecognizer (g);
    
            //svTwo.ContentSize=new System.Drawing.SizeF(1000f,100f);
            //viewThree.Frame=new CGRect(viewThree.Frame.X,viewThree.Frame.Y,viewThree.Frame.Width,viewThree.Frame.Height);
            //svTwo.TranslatesAutoresizingMaskIntoConstraints = false;
            base.ViewDidLoad ();
            // Setup keyboard event handlers
            RegisterForKeyboardNotifications();
    
        }
    
        public override void ViewDidUnload()
        {
            base.ViewDidUnload();
    
            UnregisterKeyboardNotifications();
        }
    
        NSObject _keyboardObserverWillShow;
        NSObject _keyboardObserverWillHide;
    
        protected virtual void RegisterForKeyboardNotifications ()
        {
            _keyboardObserverWillShow = NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.WillShowNotification, KeyboardWillShowNotification);
            _keyboardObserverWillHide = NSNotificationCenter.DefaultCenter.AddObserver (UIKeyboard.WillHideNotification, KeyboardWillHideNotification);
        }
    
        protected virtual void UnregisterKeyboardNotifications()
        {
            NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardObserverWillShow);
            NSNotificationCenter.DefaultCenter.RemoveObserver(_keyboardObserverWillHide);
        }
    
        protected virtual UIView KeyboardGetActiveView()
        {
            return this.View.FindFirstResponder();
        }
    
        protected virtual void KeyboardWillShowNotification (NSNotification notification)
        {
            UIView activeView = KeyboardGetActiveView();
            if (activeView == null)
                return;
    
            UIScrollView scrollView = activeView.FindSuperviewOfType(this.View, typeof(UIScrollView)) as UIScrollView;
            if (scrollView == null)
                return;
    
            RectangleF keyboardBounds = (RectangleF)UIKeyboard.FrameBeginFromNotification(notification);
    
            UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, keyboardBounds.Size.Height, 0.0f);
            scrollView.ContentInset = contentInsets;
            scrollView.ScrollIndicatorInsets = contentInsets;
    
            // If activeField is hidden by keyboard, scroll it so it's visible
            CGRect viewRectAboveKeyboard = new CGRect(this.View.Frame.Location, new CGSize(this.View.Frame.Width, this.View.Frame.Size.Height - keyboardBounds.Size.Height));
    
            RectangleF activeFieldAbsoluteFrame =(RectangleF) activeView.Superview.ConvertRectToView(activeView.Frame, this.View);
            // activeFieldAbsoluteFrame is relative to this.View so does not include any scrollView.ContentOffset
    
            // Check if the activeField will be partially or entirely covered by the keyboard
            if (!viewRectAboveKeyboard.Contains(activeFieldAbsoluteFrame))
            {
                // Scroll to the activeField Y position + activeField.Height + current scrollView.ContentOffset.Y - the keyboard Height
                CGPoint scrollPoint = new CGPoint(0.0f, activeFieldAbsoluteFrame.Location.Y + activeFieldAbsoluteFrame.Height + scrollView.ContentOffset.Y - viewRectAboveKeyboard.Height);
                scrollView.SetContentOffset(scrollPoint, true);
            }
        }
    
        protected virtual void KeyboardWillHideNotification (NSNotification notification)
        {
            UIView activeView = KeyboardGetActiveView();
            if (activeView == null)
                return;
    
            UIScrollView scrollView = activeView.FindSuperviewOfType (this.View, typeof(UIScrollView)) as UIScrollView;
            if (scrollView == null)
                return;
    
            // Reset the content inset of the scrollView and animate using the current keyboard animation duration
            double animationDuration = UIKeyboard.AnimationDurationFromNotification(notification);
            UIEdgeInsets contentInsets = new UIEdgeInsets(0.0f, 0.0f, 0.0f, 0.0f);
            UIView.Animate(animationDuration, delegate{
                scrollView.ContentInset = contentInsets;
                scrollView.ScrollIndicatorInsets = contentInsets;
            });
        }
    
        public override void DidReceiveMemoryWarning ()
        {
            base.DidReceiveMemoryWarning ();
            // Release any cached data, images, etc that aren't in use.
        }
    }
    public static class ViewExtensions
    {
    
        public static UIView FindFirstResponder (this UIView view)
        {
            if (view.IsFirstResponder)
            {
                return view;
            }
            foreach (UIView subView in view.Subviews) {
                var firstResponder = subView.FindFirstResponder();
                if (firstResponder != null)
                    return firstResponder;
            }
            return null;
        }
    
        public static UIView FindSuperviewOfType(this UIView view, UIView stopAt, Type type)
        {
            if (view.Superview != null)
            {
                if (type.IsAssignableFrom(view.Superview.GetType()))
                {
                    return view.Superview;
                }
    
                if (view.Superview != stopAt)
                    return view.Superview.FindSuperviewOfType(stopAt, type);
            }
    
            return null;
        }
    }
    

    }

    when i dismiss the keyboard final view(Where all the controls are placed ) is lifted up stick in top. like the attached pic
    i failed to did this.

Sign In or Register to comment.