Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

iOS auto-scroll

$
0
0

I have been trying to implement an automatic scroll for when any UI-elements like a textField are hidden by the iphone's keyboard. Unfortunately I seem to get a null pointer exception which I can't seem to figure out. Here are the 3 methods which I am using:

void ScrollTheView (bool move)
        {

            // scroll the view up or down
            UIView.BeginAnimations (string.Empty, System.IntPtr.Zero);
            UIView.SetAnimationDuration (0.2);

            RectangleF frame = View.Frame;

            if (move) {
                frame.Y -= scroll_amount;
            } else {
                frame.Y += scroll_amount;
                scroll_amount = 0;
            }

            View.Frame = frame;
            UIView.CommitAnimations ();
        }

        void KeyBoardUpNotification (NSNotification notification)
        {

            var visible = notification.Name == UIKeyboard.WillShowNotification;
            var keyboardFrame = visible
                ? UIKeyboard.FrameEndFromNotification (notification)
                : UIKeyboard.FrameBeginFromNotification (notification);


            // Find what opened the keyboard
            if (activeview == null)
                activeview = this.View.FindFirstResponder ();
            else
                Console.WriteLine ("Can't find activeview");
            // Bottom of the controller = initial position + height + offset      
            bottom = (activeview.Frame.Y + activeview.Frame.Height + offset);

            // Calculate how far we need to scroll
            scroll_amount = (keyboardFrame.Width - (View.Frame.Size.Height - bottom));
            //scroll_amount = -(scroll_amount);
            // Perform the scrolling
            if (scroll_amount > 0) {
                moveViewUp = true;
                ScrollTheView (moveViewUp);
            } else {
                moveViewUp = false;
            }
        }
        private void KeyBoardDownNotification(NSNotification notification)
        {
            if(moveViewUp){ScrollTheView(false);}
        }

I am receiving the keyboardNotifications like this:

// Keyboard popup
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.DidShowNotification,KeyBoardUpNotification);

// Keyboard Down
NSNotificationCenter.DefaultCenter.AddObserver
(UIKeyboard.WillHideNotification,KeyBoardDownNotification);

Everytime I run this, it seems to work fine for a little until I get an error on this line of code:

bottom = (activeview.Frame.Y + activeview.Frame.Height + offset);

It seems like my activeview is null, but I don't know why. I am also not sure whether this code needs to be in all of my viewControllers which are in need of this feature, or if it'd be enough to just have it in my rootController ?

Thank you so much for your help.


Viewing all articles
Browse latest Browse all 204402

Trending Articles