简单记录一下今天遇到的一个应用程序屏幕方向的问题。
一般情况下,应用程序都会指定方向,例如横向(landscape),或者竖向(portrait)。
那么如何设定呢?
注意:这里要区分不同版本的。
找到AppDelegate.m文件中
// The available orientations should be defined in the Info.plist file. 支持的方向需要在 Info.plist 文件中定义
// And in iOS 6+ only, you can override it in the Root View controller in the "supportedInterfaceOrientations" method.
// Only valid for iOS 6+. NOT VALID for iOS 4 / 5.
-(NSUInteger)supportedInterfaceOrientations {
//这里指定是竖向,包括两个UIInterfaceOrientationMaskPortrait 和 UIInterfaceOrientationMaskPortraitUpsideDown
// iPhone only
if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone )
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
//iPad only
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
// Supported orientations. Customize it for your own needs
// Only valid on iOS 4 / 5. NOT VALID for iOS 6.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//这里也指定是竖向
// iPhone only
if( [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone )
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
// iPad only
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
//这个方法指定是横向
// return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
看到上面的代码就知道,第一个方法是用于设定iOS6+版本的,第二个方法是设定iOS4/5版本的。
对于第一个方法中 UIInterfaceOrientationMask 根据需要选择 return
typedef enum {
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait),
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft),
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown),
UIInterfaceOrientationMaskLandscape =
(UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight),
UIInterfaceOrientationMaskAll =
(UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown),
UIInterfaceOrientationMaskAllButUpsideDown =
(UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
UIInterfaceOrientationMaskLandscapeRight),
} UIInterfaceOrientationMask;
大概就是这样啦!
