IOS 6 强制设备方向为横向 [英] IOS 6 force device orientation to landscape
问题描述
我提供了一个包含 10 个视图控制器的应用程序.我使用导航控制器来加载/卸载它们.
I gave an app with say 10 view controllers. I use navigation controller to load/unload them.
除一个外,其他所有模式都处于纵向模式.假设第 7 个 VC 在横向.我需要它在加载时以横向显示.
All but one are in portrait mode. Suppose the 7th VC is in landscape. I need it to be presented in landscape when it gets loaded.
请提出一种在 IOS 6 中强制方向从纵向变为横向的方法(并且在 IOS 5 中也能正常工作).
Please suggest a way to force the orientation go from portrait to landscape in IOS 6 (and it will be good to work in IOS 5 as well).
这是我在 之前 IOS 6 的做法:
Here is how I was doing it BEFORE IOS 6:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIViewController *c = [[[UIViewController alloc]init] autorelease];
[self presentModalViewController:c animated:NO];
[self dismissModalViewControllerAnimated:NO];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
呈现和关闭模态 VC 会迫使应用检查其方向,因此会调用 shouldAutorotateToInterfaceOrientation
.
Presenting and dismissing a modal VC was forcing the app to review its orientation, so shouldAutorotateToInterfaceOrientation
was getting called.
我在 IOS 6 中的尝试:
- (BOOL)shouldAutorotate{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeLeft;
}
加载时,控制器保持纵向.旋转设备后,方向改变就好了.但是我需要让控制器在加载时自动旋转到横向,因此用户必须旋转设备才能正确查看数据.
On load, the controller keeps staying in portrait. After rotating the device, the orientation changes just ok. But I need to make the controller to rotate automatically to landscape on load, thus the user will have to rotate the device to see the data correctly.
另一个问题:将设备旋转回纵向后,方向变为纵向,尽管我在supportedInterfaceOrientations
中仅指定了UIInterfaceOrientationMaskLandscape
.为什么会发生?
Another problem: after rotating the device back to portrait, the orientation goes to portrait, although I have specified in supportedInterfaceOrientations
only UIInterfaceOrientationMaskLandscape
. Why it happens?
此外,上述 3 个方法中的 NONE 被调用.
Also, NONE of above 3 methods are getting called.
一些(有用的)数据:
- 在我的 plist 文件中,我指定了 3 个方向 - 几乎都是颠倒的.
- 该项目是在 Xcode 4.3 IOS 5 中启动的.包括 xibs 在内的所有类都是在 Xcode 4.5 IOS 6 之前创建的,现在我使用的是最新版本.
- 在 plist 文件中,状态栏设置为可见.
- 在 xib 文件(我想要横向的文件)中,状态栏为无",方向设置为横向.
感谢任何帮助.谢谢.
推荐答案
好的,伙计们,我会发布我的解决方案.
Ok, folks, I will post my solution.
我有什么:
- 基于视图的应用程序,具有多个视图控制器.(它是基于导航的,但由于方向问题,我不得不让它基于视图).
- 所有视图控制器都是纵向的,除了一个 - LandscapeLeft.
任务:
- 我的一个视图控制器必须自动旋转为横向,无论用户如何握持设备.所有其他控制器都必须是纵向的,离开横向控制器后,应用必须强制旋转为纵向,无论用户如何握持设备.
- 这必须像在 IOS 6.x 上一样在 IOS 5.x 上工作
去吧!
(更新删除了@Ivan Vučica 建议的宏)
(Update Removed the macros suggested by @Ivan Vučica)
在你所有的 PORTRAIT 视图控制器中,像这样覆盖自动旋转方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
您可以看到两种方法:一种适用于 IOS 5,另一种适用于 IOS 6.
You can see the 2 approaches: one for IOS 5 and another For IOS 6.
与您的 LANDSCAPE 视图控制器相同,但有一些添加和更改:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
[image_signature setImage:[self resizeImage:image_signature.image]];
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
-(BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
[image_signature setImage:[self resizeImage:image_signature.image]];
return UIInterfaceOrientationMaskLandscapeLeft;
}
注意:要在 IOS 5 中强制自动旋转,您应该添加:
ATTENTION: to force autorotation in IOS 5 you should add this:
- (void)viewDidLoad{
[super viewDidLoad];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0)
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
}
类似地,在您离开 LANDSCAPE 控制器后,无论您加载什么控制器,您都应该为 IOS 5 再次强制自动旋转,但现在您将使用 UIDeviceOrientationPortrait
,因为您转到 PORTRAIT 控制器:>
Analogically, after you leave the LANDSCAPE controller, whatever controller you load, you should force again autorotation for IOS 5, but now you will use UIDeviceOrientationPortrait
, as you go to a PORTRAIT controller:
- (void)viewDidLoad{
[super viewDidLoad];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0)
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO];
}
现在最后一件事(有点奇怪) - 您必须根据 IOS 更改从控制器切换到另一个控制器的方式:
创建一个 NSObject 类Schalter"(德语中的Switch").
Make an NSObject class "Schalter" ("Switch" from German).
在 Schalter.h 中说:
In Schalter.h say:
#import <Foundation/Foundation.h>
@interface Schalter : NSObject
+ (void)loadController:(UIViewController*)VControllerToLoad andRelease:(UIViewController*)VControllerToRelease;
@end
在 Schalter.m 中说:
In Schalter.m say:
#import "Schalter.h"
#import "AppDelegate.h"
@implementation Schalter
+ (void)loadController:(UIViewController*)VControllerToLoad andRelease:(UIViewController*)VControllerToRelease{
//adjust the frame of the new controller
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect windowFrame = [[UIScreen mainScreen] bounds];
CGRect firstViewFrame = CGRectMake(statusBarFrame.origin.x, statusBarFrame.size.height, windowFrame.size.width, windowFrame.size.height - statusBarFrame.size.height);
VControllerToLoad.view.frame = firstViewFrame;
//check version and go
if (IOS_OLDER_THAN_6)
[((AppDelegate*)[UIApplication sharedApplication].delegate).window addSubview:VControllerToLoad.view];
else
[((AppDelegate*)[UIApplication sharedApplication].delegate).window setRootViewController:VControllerToLoad];
//kill the previous view controller
[VControllerToRelease.view removeFromSuperview];
}
@end
现在,这就是您使用 Schalter 的方式(假设您从仓库控制器转到产品控制器):
#import "Warehouse.h"
#import "Products.h"
@implementation Warehouse
Products *instance_to_products;
- (void)goToProducts{
instance_to_products = [[Products alloc] init];
[Schalter loadController:instance_to_products andRelease:self];
}
bla-bla-bla your methods
@end
当然你必须释放instance_to_products
对象:
- (void)dealloc{
[instance_to_products release];
[super dealloc];
}
嗯,就是这样.不要犹豫,投反对票,我不在乎.这是为那些正在寻找解决方案的人准备的,而不是为了声誉.干杯!萨瓦·马扎雷.
这篇关于IOS 6 强制设备方向为横向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!