Search code examples
iphonecocoa-touchuiinterfaceorientationautorotate

Auto orientation


In my project I am required to implement the orientation feature of iPhone. For this I have created two xib files and one UIViewController controller file, one for landscape and other for portrait. I have taken one UIViewController because the content of the xib files are same.

Now I want to load the respective xib file depending on the rotation and the content should remain populated in the TextField when the specific file loads. How should I implement this?

CODE:

if( orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) 
{
   NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"landscapeView" owner:self options:nil]; UIView *viewItem = (UIView *)[nibArray objectAtIndex:0]; self.view = viewItem;
}
else if(orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) 
{
   //same above lines only different nib name 
}

Solution

  • You can check in your init method of UIViewController device orientation using following code

    if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])
    {
         // load view for landscape mode
    }else
    {
         // load view for portrait mode
    }
    

    And choose .xib file that correspond to current orientation.

    Hope, this will work.