1

I am trying to add a sub view to the main view when a button is tapped.

  • The main view supports both landscape modes and portrait mode.

  • The sub view supports only landscape mode.

How do I go about doing this?

Mahesh Babu
  • 3,395
  • 9
  • 48
  • 97

4 Answers4

4

In - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration you could apply a transformation to the view you wish to not rotate:

view.transform = CGAffineTransformMakeRotation(M_PI/2);

You could put this in an animation block so that it follows the rotation animation:

[UIView setAnimationDuration:duration];
[UIView beginAnimations:nil context:NULL];
view.transform = CGAffineTransformMakeRotation(M_PI/2);
[UIView commitAnimations];

The rotation occurs around the views origin, which can make positioning it tricky. To position the view correctly I would first ignore the rotation and ensure that the struts and springs are set such that the view is in the correct position without the rotation. Once the view is positioned correctly re-enable the rotation and adjust the view's frame's origin so that the view is where you want it to be. It's possible to apply a translation transformation instead of adjusting the frame (transformations can be combined with CGAffineTransformConcat). Use which ever approach produces the most readable code.

Be careful, this kind of view manipulation can get confusing. Technically, the view that you do not want to rotate is actually the view that is rotating! You also may need to change the rotation amount depending on the start and end orientations.

Benedict Cohen
  • 11,912
  • 7
  • 55
  • 67
1

Take a tag value to all the landscape views which ever you wanted to add and before adding them to the main view check for the tag value and add them.

You can know the current orientation by maintaining a flag whenever - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation is called.

Gina
  • 132
  • 1
  • 1
  • 3
0

Did you follow this link

iPhone app in landscape mode, 2008 systems

or just add this line of code in your .m file of the viewController which you want to display in landscape mode

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        return YES;
    }
    return NO;
}
Community
  • 1
  • 1
Robin
  • 10,011
  • 5
  • 49
  • 75
  • hey i think u did n't get what i am asking,if i use this code it supports all the orientations,But i need to prevent this orientation to only one sub view. – Mahesh Babu Feb 03 '11 at 11:49
  • return YES is when the view `should rotate` and NO is when view `should not rotate` – Robin Feb 04 '11 at 01:28
  • in the if condition you can have any type of orientation support like `UIInterfaceOrientationLandscapeLeft` and portrait mode too – Robin Feb 04 '11 at 01:34
0

I resolve this by rotating the screen and adjusting the view frame.

if (toorientation == UIInterfaceOrientationPortrait) {
        sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(0));
    }
    else if (toorientation == UIInterfaceOrientationPortraitUpsideDown){
        sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(180));
    }
    else if (toorientation == UIInterfaceOrientationLandscapeLeft){
        sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(-90));
    }
    else if (toorientation == UIInterfaceOrientationLandscapeRight){
        sub_view.transform = CGAffineTransformMakeRotation(degreesToRadin(90));
    }
Mahesh Babu
  • 3,395
  • 9
  • 48
  • 97