Search code examples
iphoneiosuinavigationcontrollerrotation

Best practices for navigations in landscape and portrait mode in UINavigationController


I am developing an iPad application which supports both landscape as well as portrait orientation.I am having two different nib files for my rootviewController. Here is the scenario of the issue -
1.Select an item from root view in portait mode, pushes the next view in portrait.
2.Rotate the device
3.Press back button on navigation bar
The view is loaded from the stack is portait view of rootViewController. While device is still in landscape mode.
Please suggest the solution to handle above issue.
Also, please suggest the best practices to follow while handling device rotations.


Solution

  • Use notifications in following methods and set the coordinates in receivedRotate method.

    -(void)viewWillAppear:(BOOL)animated
    {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    
    }
    
    -(void)viewWillDisappear:(BOOL)animated
    {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
    }
    

    For example in receivedRotate you can set your tableview's coordinates as :

    if (orientation == UIDeviceOrientationLandscapeLeft||orientation==UIDeviceOrientationLandscapeRight)
        {
            [tblView reloadData];
            tableX=70.0;
    
        }
        else
        {
            [tblView reloadData];
            tableX=48.0;
    }  
    

    Also call recieveRotate in viewDidLoad which is very important.