0
votes

I have an iPad app that has 2 views each with its own View Controller. Apple states they suggest iPad apps be able to rotate in all directions. I used this technique to position the elements in my storyboard on rotation changes:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    //[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        studentNumberButton.frame = CGRectMake(30, 1, 158, 155);
        studentNumberLabel.frame = CGRectMake(173, 57, 347, 43);
        studentLabel.frame = CGRectMake(528, 56, 67, 43);

        // Other code goes below...

I did this for each view and it works fine for all rotations. However the issue comes up when I say, have the the iPad on View Controller 1 in landscape and press a button to show View Controller 2, View Controller 2 comes up just fine but is in Portrait mode. Same happens if I am in landscape mode for View Controller 2 and go to View Controller 1. If I am in portrait mode, everything is fine for both views.

How can i make it so when I switch views, the view knows what orientation the device is in and rotates accordingly?

Thanks!

1

1 Answers

0
votes

You need to add a viewWillAppear method as follows:

- (void)viewWillAppear:(BOOL)animated {
if (([self interfaceOrientation] == UIDeviceOrientationLandscapeLeft)|| ([self interfaceOrientation] == UIDeviceOrientationLandscapeRight)) {

    studentNumberButton.frame = CGRectMake(30, 1, 158, 155);
    studentNumberLabel.frame = CGRectMake(173, 57, 347, 43);
    studentLabel.frame = CGRectMake(528, 56, 67, 43);
    ...

} else {
    ...
}

}