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种方法的无也会被调用。
Also, NONE of above 3 methods are getting called.
一些(有用的)数据:
- 在我的plist文件中,我指定了3个方向 - 除了上升外
- 该项目是在Xcode 4.3 IOS 5中启动的。包括xib在内的所有类都是在Xcode 4.5 IOS 6之前创建的,现在我使用的是最后一个版本。
- 在plist文件中,状态栏设置为可见。
- 在xib文件中(我希望在横向上)状态栏为无,方向设置为到景观。
- In my plist file I have specified 3 orientations - all but upside down.
- The project was started in Xcode 4.3 IOS 5. All classes including xibs were created before Xcode 4.5 IOS 6, now I use the last version.
- In plist file the status bar is set to visible.
- In xib file (the one I want to be in landscape) the status bar is "None", the orientation is set to landscape.
任何帮助表示赞赏。谢谢。
Any help is appreciated. Thanks.
推荐答案
好的,伙计们,我会发布我的解决方案。
Ok, folks, I will post my solution.
我有什么:
- 基于视图的应用程序,带有多个视图控制器。 (这是基于导航的,但由于方向问题,我不得不基于视图)。
- 所有视图控制器都是纵向的,除了一个 - landscapeLeft。
任务:
- 我的一个视图控制器必须自动旋转到横向,无论用户如何握住设备。所有其他控制器必须是纵向的,并且在离开横向控制器后,应用程序必须强制旋转为纵向,无论用户如何握住设备。
- 这必须像以前一样工作IOS 6.x和IOS 5.x一样
Go!
(更新删除了@IvanVuč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;
}
您可以看到2种方法:一种用于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(假设您从Warehouse控制器转到Products控制器):
#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];
}
嗯,就是这样。不要犹豫,我不在乎。这适用于那些寻求解决方案而非声誉的人。
干杯!
Sava Mazare。
这篇关于IOS 6强制设备定位到景观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!