[英]IOS 6 orientation issue in universal app
在我的通用应用程序中,我需要为iPhone和iPad处理不同的方向。 对于ipad,我需要允许横向放置,而对于iphone肖像则需要单独放置。 我首先返回下面的代码
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
在IOS 5中工作正常,但在IOS 6中根本不触发自动旋转方法。 之后,我将方法更改为
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationMaskPortrait;
}
甚至在IOS 6中根本没有触发这种方法。
我的plist设置是
我需要同时处理IOS 5和IOS 6的两个方向[iPhone-portrait,iPad-landscape]。请指导我解决此问题。
您的rootviewcontroller是UINavigation控制器还是UITabbarcontroller? 如果是这样,如果您在视图控制器中调用这些方法,则这些方法将无法工作。
因此,在这些容器视图控制器上创建一个目标C类别,并将其添加到您的项目中。
@implementation UINavigationController (autorotate)
-(NSUInteger)supportedInterfaceOrientations
{
//make the check for iphone/ipad here
if(IPHONE)
{
return UIInterfaceOrientationMaskPortrait;
}
else
{
return UIInterfaceOrientationMaskLandscape;
}
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotate
{
return NO;
}
将此方法添加到您的应用程序委托中,使其100%工作
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window // iOS 6 autorotation fix
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return UIInterfaceOrientationPortraitUpsideDown;
}
else
return UIInterfaceOrientationMaskAll;
}
您想要的是以下内容:
#pragma mark - iOS 5.1 and under Rotation Methods
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation; {
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
else{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
}
#pragma mark - iOS 6.0 and up Rotation Methods
- (BOOL)shouldAutorotate; {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations; {
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}
else{
return UIInterfaceOrientationMaskLandscape;
}
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation; {
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
return UIInterfaceOrientationPortrait;
}
else{
return UIInterfaceOrientationLandscapeLeft;
}
}
您必须确保2折 。
首先 ,这是在RootViewController
实现的。 如果要将UIViewController
嵌入到UINavigationController
(或UITabBarController
)内部,则必须创建一个实现这些方法并使用它的自定义UINavigationController
(或自定义UITabBarController
)类。
其次 ,您必须确保Info.plist
文件中具有受支持的方向。 否则,系统将覆盖这些方法的返回值( 注意:您也可以通过单击目标“摘要”页面中的按钮来轻松更改这些值)。
希望对您有所帮助!
这是Pradeep答案的快速版本。 您无需继承所有导航控制器的子类即可获得想要的工作(也就是,仅针对iPhone关闭横向)
只需将其添加到您的应用程序委托中:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
} else {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.