Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling viewWillTransitionToSize: manually

I currently have a UiViewController called Page2 Now if the orientation of the Iphone is changed to landscape the following method is called which is well and good

    -(void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator 
    {
         /*The orientation of the phone just changed*/
    }

Now My issue is that if the user is already in landscape mode in ViewController page1 and goes to UIViewController Page2 the above method is not called. I know I can check the current orientation of the phone using the following code

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if(orientation == UIInterfaceOrientationLandscapeLeft)
    {
        //Its in landscape mode
    }

However I do not know what parameters to pass for size to the method (that gets called automatically only when the orientation changes) I would like to call this method manually if I detect its in landscape mode. Any suggestions on how to obtain the current size to pass to the method below in which the width would be greater than the height (since its in landscape mode)

 -(void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator ;

I tried doing this

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
    {
        [self viewWillTransitionToSize:self.view.frame.size withTransitionCoordinator:nil];
    }

However the height was greater than the width indicating it was still in portrait mode ... What am i doing wrong ? How can I call this method manually and pass the current size in which width is greater than the height ?

Update:

I would like to give a little more background. I currently have two UIViewControllers PageOne and PageTwo. Now PageTwo has something like this in it for orientation control. The control flow goes from PageOne to PageTwo

//When device rotates
-(void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{

    DKTuner* sensitivityTuner;
    if (size.width > size.height)
    {   //Yes this is the landscape mode - Realign some controls to a different position.
        .....        
    }
    else
    {
        // this is portrait mode
        .....
    } 
}

Now the logic of my code in PageTwo assumes that the user entered it in Portrait Mode. However in reality that will not always be true because the user could enter pageTwo while in LandscapeMode. In that case the controls dont readjust to landscape mode as the method of reorientation does not get called in pageTwo. My question is how can I call the reorientation method viewWillTransitionToSize manually and pass a size parameter in which the size width is greater than the height ?

like image 330
MistyD Avatar asked May 08 '15 02:05

MistyD


1 Answers

Whatever you're doing in viewWillTransitionToSize:withTransitionCoordinator:, you should probably do in viewWillLayoutSubviews or viewDidLayoutSubviews instead. Those are sent both on a rotation and when the view controller's view first comes onto the screen, and UIKit updates the view's frame before calling viewWillLayoutSubviews.

like image 161
rob mayoff Avatar answered Nov 05 '22 05:11

rob mayoff