1

I have UIViewController which contains UICollectionView. On clicking on any of the UICollectionViewCell I show the detailed contents of the cell in a modal view. I have added objects (UILabel, UIImage etc.) in modal view through storyboard. I resize frames when device orientation is changed using NSNotificationCenter:

In viewDidLoad method of modal controller:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
NSNotificationCenter* notifCenter = [NSNotificationCenter defaultCenter];
[notifCenter addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:nil];

orientationChanged content:

-(void) orientationChanged {
    if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        self.productImage.frame = CGRectMake(61, 5, 124, 138);
        self.siteLogo.frame = CGRectMake(61, 146, 124, 90);
        self.productName.frame = CGRectMake(220, 5, 150, 102);
        self.productPrice.frame = CGRectMake(220, 120, 124, 21);
    }
    else {
        self.productImage.frame = CGRectMake(61, 5, 124, 138);
        self.siteLogo.frame = CGRectMake(61, 146, 124, 90);
        self.productName.frame = CGRectMake(20, 244, 207, 102);
        self.productPrice.frame = CGRectMake(61, 354, 124, 21);
    }
}

Now when I start my app in portrait mode and click of any of the UICollectionViewCell the modal view shows up correctly, also now when I change to landscape mode (without dismissing modal view) I am able to resize frames due to above method.

Problem is when I start my app in landscape mode and then I click on UICollectionViewCell the modal view shows up in portrait mode. I want it to be in landscape mode with correctly resizing frames. I tried setting frames in viewDidLayoutSubviews & in viewDidAppear but of no use.

content of viewDidLayoutSubviews:

-(void)viewDidLayoutSubviews{
    self.productImage.autoresizingMask = 0;
    self.productPrice.autoresizingMask = 0;
    self.productName.autoresizingMask = 0;
    self.siteLogo.autoresizingMask = 0;
    if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        self.productImage.frame = CGRectMake(61, 5, 124, 138);
        self.siteLogo.frame = CGRectMake(61, 146, 124, 90);
        self.productName.frame = CGRectMake(220, 5, 150, 102);
        self.productPrice.frame = CGRectMake(220, 120, 124, 21);
    }
    else {
        self.productImage.frame = CGRectMake(61, 5, 124, 138);
        self.siteLogo.frame = CGRectMake(61, 146, 124, 90);
        self.productName.frame = CGRectMake(20, 244, 207, 102);
        self.productPrice.frame = CGRectMake(61, 354, 124, 21);
    }
}

Thanks in Advance.

Pravin Gadakh
  • 603
  • 1
  • 9
  • 19
  • Did you tried using `willRotateToInterfaceOrientation:duration:` – Abhishek Bedi Mar 11 '14 at 19:53
  • @AbhishekBedi Yes I tried it, but results still the same. – Pravin Gadakh Mar 11 '14 at 19:55
  • Did you set `didAutorotateToInterfaceOrientation` method to YES. Please post the entire class code. Hope you are calling method like this : ` [self.navigationController presentViewController:webVC animated:YES completion:^{ }];` – Abhishek Bedi Mar 11 '14 at 20:01
  • Check this also : http://stackoverflow.com/a/12555205/667586 – Abhishek Bedi Mar 11 '14 at 20:08
  • I have added modal segue between two controllers and call performSegue in didSelectItemAtIndexPath method of UICollectionView – Pravin Gadakh Mar 11 '14 at 20:21
  • Use this in your modal VC : // Returns interface orientation masks. `- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0){ return UIInterfaceOrientationLandscapeLeft; }` – Abhishek Bedi Mar 11 '14 at 20:29

1 Answers1

0

As per https://stackoverflow.com/a/12555205/667586

Modal ViewControllers no longer get rotation calls in iOS 6: The willRotateToInterfaceOrientation:duration:, willAnimateRotationToInterfaceOrientation:duration:, and didRotateFromInterfaceOrientation: methods are no longer called on any view controller that makes a full-screen presentation over itself—for example those that are called with: presentViewController:animated:completion:.

You can let the view controller that presents your modal view controller inform it of rotation. Also, now you use: presentViewController:animated:completion: to present the view controller. presentModalViewController:animated: is deprecated which you use in the code.

Write this code : In Modal View Controller :

// Returns interface orientation masks.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0){
return UIInterfaceOrientationLandscapeLeft;
}

In Calling View Controller :

MyVC *webVC = [[MyVC alloc] init];
[self.navigationController presentViewController:myVC animated:YES completion:^{
    [myVC shouldAutorotate];
}];
Community
  • 1
  • 1
Abhishek Bedi
  • 5,205
  • 2
  • 36
  • 62