Question

I am trying to show UIPickerView on click on UITextField. I am using a simple method to show the picker as follows:

-(void) showPickerView {

    [self.view resignFirstResponder];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.50];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
    if(iOSDeviceScreenSize.height == 480){

        CGRect frame = self.picker.frame;
        frame.origin.y = 270;
        self.picker.frame = frame;
        frame = self.toolBar.frame;
        frame.origin.y = 226.0;
        self.toolBar.frame = frame;

    } else if(iOSDeviceScreenSize.height == 568){

        CGRect frame = self.picker.frame;
        frame.origin.y = 239.0;
        self.picker.frame = frame;
        frame = self.toolBar.frame;
        frame.origin.y = 195.0;
        self.toolBar.frame = frame;

    }

    [UIView commitAnimations];

}

It works great if I have not scrolled down. But when I scroll down, my picker is placed on fix position as mentioned in showPickerView method. Now how I can manage it that my picker appears at bottom every time.

Was it helpful?

Solution

Solution 1:

Add your pickerview to your window.

Solution 2:

Implement the scrollview delegate and adjust the frame continuously.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{


    CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
    if(iOSDeviceScreenSize.height == 480){

        CGRect frame = self.picker.frame;
        frame.origin.y = 270;
        self.picker.frame = frame;
        frame = self.toolBar.frame;
        frame.origin.y = 226.0;
        self.toolBar.frame = frame;

    } else if(iOSDeviceScreenSize.height == 568){

        CGRect frame = self.picker.frame;
        frame.origin.y = 239.0;
        self.picker.frame = frame;
        frame = self.toolBar.frame;
        frame.origin.y = 195.0;
        self.toolBar.frame = frame;

    }



}

OTHER TIPS

I can just suppose that you are showing your picker inside of a scroll view, right? And if so, then your picker frame is relative to UIScrollView, not to a screen view. So you have to take into account your scroll view offset. Something like that:

    CGRect frame = self.picker.frame;
    frame.origin.y = self.scrollvew.contentOffset.y + 270;

Otherwise you can show your picker outside of the UIScrollView (just add it to a screen view).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top