1. In appDelegate, there are two ways to add a view to a window,
[Cpp]
Self. window. rootViewController = self. view;
[Self. window addSubview: self. view. view];
Self. window. rootViewController = self. view;
[Self. window addSubview: self. view. view];
However, if the second method is used, setting screen rotation in ios6.0 does not have any effect. The first method must be used. There is no such difference in versions earlier than ios6.0.
2. enable screen rotation in all directions
Before iOS6.0: You only need this method to return yes.
[Cpp] www.2cto.com
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation
{
Return YES;
}
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation
{
Return YES;
} In iOS6.0, these three methods must be used together.
[Cpp]
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation
{
Return (toInterfaceOrientation! = UIInterfaceOrientationMaskPortraitUpsideDown );
}
-(BOOL) shouldAutorotate
{
Return YES;
}
-(NSUInteger) supportedInterfaceOrientations
{
Return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation
{
Return (toInterfaceOrientation! = UIInterfaceOrientationMaskPortraitUpsideDown );
}
-(BOOL) shouldAutorotate
{
Return YES;
}
-(NSUInteger) supportedInterfaceOrientations
{
Return UIInterfaceOrientationMaskAllButUpsideDown;
} Of course, you can change the above return value to no if you disable the rotation of the screen in all directions.
Before iOS6.0:
[Cpp]
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation
{
Return NO;
}
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation
{
Return NO;
} IOS6.0
[Cpp]
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation
{
Return (toInterfaceOrientation = UIInterfaceOrientationPortrait );
}
-(BOOL) shouldAutorotate
{
Return NO;
}
-(NSUInteger) supportedInterfaceOrientations
{
Return UIInterfaceOrientationMaskPortrait; // only this direction is supported (normal direction)
}
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation
{
Return (toInterfaceOrientation = UIInterfaceOrientationPortrait );
}
-(BOOL) shouldAutorotate
{
Return NO;
}
-(NSUInteger) supportedInterfaceOrientations
{
Return UIInterfaceOrientationMaskPortrait; // only this direction is supported (normal direction)
}
Common Methods for screen Rotation
[Cpp]
// Automatically called before view Rotation
-(Void) willRotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation duration :( NSTimeInterval) duration {
NSLog (@ "automatically called before view rotation ");
}
// It is automatically called when the rotation direction of the view changes.
-(Void) willAnimateRotationToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation duration :( NSTimeInterval) duration
{
NSLog (@ "automatically called when the view rotation direction changes ");
}
// The view is automatically called after rotation.
-(Void) didRotateFromInterfaceOrientation :( UIInterfaceOrientation) fromInterfaceOrientation {
NSLog (@ "automatically called after view rotation ");
}
// Automatically called before view Rotation
-(Void) willRotateToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation duration :( NSTimeInterval) duration {
NSLog (@ "automatically called before view rotation ");
}
// It is automatically called when the rotation direction of the view changes.
-(Void) willAnimateRotationToInterfaceOrientation :( UIInterfaceOrientation) toInterfaceOrientation duration :( NSTimeInterval) duration
{
NSLog (@ "automatically called when the view rotation direction changes ");
}
// The view is automatically called after rotation.
-(Void) didRotateFromInterfaceOrientation :( UIInterfaceOrientation) fromInterfaceOrientation {
NSLog (@ "automatically called after view rotation ");
}