iosiphoneuigesturerecognizeruipickerviewuipickerviewcontroller

How to properly disable UIPickerView component scrolling?


Aster finding myself in the situation of wanting to disable a UIPickerView component and not finding any answers that uses the default functionality but tries to go around it by using labels or multiple UIPickerViews, I start poking around the UIPickerView structure in order to find a more direct approach to solve the problem by using the OS already present functionality.


Solution

  • So these is my answer, what I found by trial and error, and the way that I am using it:

    The method:

    - (void)dissableUIPickerViewComponent:(int)componentToDissable forPickerView:(UIPickerView *)pickerView{
    
        NSArray *pickerViews = [[NSArray alloc] initWithArray:[pickerView subviews]];
        UIView *mainSubview;
        for (int count = 0; count < pickerViews.count; count++) {
            UIView *temp = [pickerViews objectAtIndex:count];
            if (temp.frame.size.height > 100) {
                mainSubview = temp;
            }
    
        }
    
        NSArray *componentSubview = [mainSubview subviews];
        while ([[[[componentSubview objectAtIndex:componentToDissable] subviews] firstObject] superview].gestureRecognizers.count) {
            [[[[[componentSubview objectAtIndex:componentToDissable]subviews]firstObject]superview] removeGestureRecognizer:[[[[[componentSubview objectAtIndex:componentToDissable]subviews]firstObject]superview].gestureRecognizers objectAtIndex:0]];
        }
    
    }
    

    Best place to call:

    - (void)viewDidAppear:(BOOL)animated{
       [self dissableUIPickerViewComponent:0 myPickerView];
    }
    

    I hope these will help others that found themself in the same position that I did :)