Search code examples
iospickeruipicker

UIPicker & iOS 8


I created a UIPickerView in code only when it appears that iOS 8 the view the item is positioned at the top. How do I get it down? This is the code used:

                show actionsheet
                pickerActionSheet = [self actionSheetSimulationWithPickerView:picker withToolbar:pickerViewToolbar];

                // set frames on different orientation
                if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
                    picker.frame = CGRectMake(0, 32, SCREEN_HEIGHT, 162);
                    pickerViewToolbar.frame = CGRectMake(0, 0, SCREEN_HEIGHT, 32);
                    [pickerActionSheet setBounds:CGRectMake(0, 0, SCREEN_HEIGHT, 366)];
                }
                else{
                    picker.frame = CGRectMake(0, 44, SCREEN_WIDTH, 216);
                    pickerViewToolbar.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
                    [pickerActionSheet setBounds:CGRectMake(0, 0, SCREEN_WIDTH, 496)];
                }

Solution

  • if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
        [pickerActionSheet setBounds:CGRectMake(0, SCREEN_WIDTH-194, SCREEN_HEIGHT, 194)];
        pickerViewToolbar.frame = CGRectMake(0, 0, SCREEN_HEIGHT, 32);
        picker.frame = CGRectMake(0, 32, SCREEN_HEIGHT, 162);
    
    } else {
       [pickerActionSheet setBounds:CGRectMake(0, SCREEN_HEIGHT-216, SCREEN_WIDTH, 260)];
       pickerViewToolbar.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
       picker.frame = CGRectMake(0, 44, SCREEN_WIDTH, 216);
    }
    
    // CGRectMake(start point x, start point y, width, height)
    

    After launching the iPhone 5 (taller screen), when you need something to start from the bottom, you need to do SCREEN_HEIGHT-objectHeight. If you want it to start from top you start with 0.

    Now the iPhone 6 and 6+ (wider screen) is here, you must do with the left and right side, left you start with 0 and right you start with SCREEN_WIDTH-objectWidth.

    So from now on, no one will be using width as just 320. If you want it the whole screen width, we all need to use SCREEN_WIDTH.