Search code examples
xamarinscrollxamarin.iosuipickerview

How to stop UIPickerView during scrolling by pressing a button - Xamarin.IOS


I am developing Xamarin.ios project.

I have created UIPickerView with number 1 to 10000. It scrolls well.

Here is my need. I need to stop the UIPickerView while scrolling when a button is pressed and want to get the stoped position value.

Please help me to solve this. I have researched a lot but not get solved.


Solution

  • A UIPickerView contains a UIScrollView as one of its subviews, so if you recursively search for that view, and then test to see if it is still scrolling (animating) due to being "flinged", you can stop it dead in it tracks by setting the content offset to the current content offset.

    public void StopScrolling(UIPickerView view)
    {
        void FindViews(UIView subView)
        {
            foreach (var item in subView.Subviews)
            {
                switch (item)
                {
                    case UIScrollView sv:
                        if (sv.Dragging)
                            sv.SetContentOffset(sv.ContentOffset, false);
                        break;
                    default:
                        FindViews(item);
                        break;
                }
            }
        }
        FindViews(view);
    }