Search code examples
ipadorientationuipickerviewautorotatecgrectmake

How to adjust subviews position on screen when device changes its orientation?


I am adding pickerview on UItableviewcontroller when user taps on row. I am using following code to display it. [self.view.window addSubview:languagePicker];

    CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
    CGSize pickerSize = CGSizeMake(768.0, 216.0);
    CGRect startRect = CGRectMake(0.0,
                                  screenRect.origin.y + screenRect.size.height,
                                  pickerSize.width, pickerSize.height);
    self.languagePicker.frame = startRect;

    // compute the end frame
    CGRect pickerRect = CGRectMake(0.0,
                                   screenRect.origin.y + screenRect.size.height - pickerSize.height,
                                   pickerSize.width,
                                   pickerSize.height);


        self.languagePicker.frame=startRect;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationTransition:UIViewAnimationOptionTransitionNone forView:self.view.window cache:YES];
        [self.languagePicker setFrame:pickerRect];
        [UIView commitAnimations];

It seem to work perfect when device is in portrait orientation but as soon as the orientation changes everything adjust itself except the added picker view.

I can get device orientation, remove old view and add new one according to orientation but I am not sure whether its a good way to do it or not. can I do it any other effective way ?

Sumit


Solution

  • It was stupid mistake, instead of putting that pickerview on view itself I just dragged it and put it in xib. Putting it in view itself solved the problem.