Question

I have been working to find a solution so I have a custom designs for portrait and landscape mode.

Everything works in the simulator but I am not sure if I have done the work correctly.

I have set up two view controllers, one for portrait (ViewController) and another for landscape (landScapeViewController).

ViewController.h I have added:

@property (readwrite, assign) BOOL isShowingLandscapeView;
@property (readwrite, assign) BOOL previousOrientation;

ViewController.m I have added:

-(void)awakeFromNib
{
    _isShowingLandscapeView = NO;
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                       selector:@selector(orientationChanged:)           name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
        !_isShowingLandscapeView)
    {
        [self performSegueWithIdentifier:@"DisplayAlternateView" sender:self];
        _isShowingLandscapeView = YES;

   }
    else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
             _isShowingLandscapeView)

    {
            [self dismissViewControllerAnimated:YES completion:nil];
            _isShowingLandscapeView = NO;
        }
    }

After this I have Modaled the two ViewControllers.

Can someone please tell me if I am doing it correctly?

Was it helpful?

Solution

Use following way, you make mistake in registering notification :

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationIsChanged) name:UIDeviceOrientationDidChangeNotification object:nil]; 

- (void) orientationIsChanged 
{
    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || 
        ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight))
         {
                // Do your stuff for LandScape  orientation
    } 
    else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) 
    {
        // Do your stuff for Portrait orientation
    }   
}

OR Simply detect as follow :

if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) 
{
        // Do your stuff for LandScape  orientation
} 
else if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) 
{
        // Do your stuff for Portrait orientation
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top