7
votes

Can anyone give an example of how to rotate the view of a monotouch application from portrait to landscape and vice versa?

3

3 Answers

8
votes

If you set the geometry of your ui elements in Interface Builder, then make sure you set the Autosizing attributes in the Size Inspector (Cmd+3). You can then see how the view will look after rotating by clicking the little "Rotate" button in the upper left of the title bar of your view in Interface Builder.

Once you have all that set up, just override the following method in your ViewController:

public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
    return true;
}

Now in the simulator, you can rotate all you want and your UI will auto-adapt.

1
votes

Just so everyone knows, it is now on the views Transform object:

View.Transform.Rotate(3.14159f * 0.5f);

For all you dotnet guys like me that don't want to use anything that is CGAffineTransform, thinking it will break other things, just try it. Worked great for my instance. Rotated the NavigationController.View and everything inside it is perfect.

1
votes

This is what you can try out.

If you want to change the orientation of the view while touching on the view , then follow the steps

  1. Implement the touchesBegan method inside the view.

  2. In the touchesBegan method check the current device orientation, [[UIDevice currentDevice] orientation];

  3. If you want to change the orientation then use the CGAffineTransformation method on the view as viewRef.transform = CGAffineTransformMakeRotation(angle);