开发者

How to rotate screen to landscape?

How to rotate screen to landscape ?

Can you suggest simple cod开发者_如何学编程e ?


It's trickier than you first think! After much discussion this blog post (with a link to further discussion afterwards) contains the cleanest answer:

How to Switch to Landscape Mode at will


in the uiViewController you should have the method

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
 return YES;
}

in order to rotate automatically.

If your app has an uiTabBarController then you have to subclass the UITabBarController and add the method to it also.Something like this:

@interface MyTabBarController : UITabBarController {

}

@end

#import "MyTabBarController.h"

@implementation MyTabBarController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

@end


This howto describes how you can enable auto rotation in your application: http://developer.apple.com/iphone/library/codinghowtos/UserExperience/index.html#GENERAL-HANDLE_AUTOROTATION_OF_MY_USER_INTERFACE

This howto describes how you can start your application in landscape mode: http://developer.apple.com/iphone/library/codinghowtos/UserExperience/index.html#GENERAL-START_MY_APPLICATION_IN_LANDSCAPE_MODE


Thanks SorinA. Needing UITabBarController shouldAutorotateToInterfaceOrientation: gave me all sorts of frustrations since it is not a class that I was subclassing for any reason (i.e., the default functionality was fine for my app).

As a lighter-weight solution than subclassing I used a category (maybe its all the same, but it seems lighter weight).

@interface UITabBarController (WithRotation)
@end

@implementation UITabBarController (WithRotation)
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation {
    return YES;
}
@end

p.s. If you only want to support some orientations, test the interfaceOrientation and only return YES in the appropriate orientations.

p.p.s. What is the info.plist Supported interface orientations for? The only thing that seems to matter is what is returned from shouldAutorotateToInterfaceOrientation:


Just implement this in your view controller file, and you're done.

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
{
     return YES;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜