好的,大家好,我将发布我的解决方案。
我的情况如下:
- 一个基于视图的应用程序,有几个视图控制器。(它是基于导航的,但由于方向问题,我不得不使它基于视图)。
- 所有视图控制器都是竖屏的,除了一个——横屏左侧。
任务:
- 我的一个视图控制器必须自动旋转到横屏,无论用户如何持握设备。所有其他控制器必须是竖屏的,并且在离开横屏控制器后,应用程序必须强制旋转到竖屏,无论用户如何持握设备。
- 这必须在IOS 6.x和IOS 5.x上都能正常工作。
开始吧!
(更新 删除了@Ivan Vučica建议的宏)
在您所有的竖屏视图控制器中重写自动旋转方法,像这样:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
你可以看到两种方法:一种适用于IOS 5,另一种适用于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中强制自动旋转,请添加以下内容:
- (void)viewDidLoad{
[super viewDidLoad];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0)
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
}
类比地说,当你离开LANDSCAPE控制器后,无论你加载哪个控制器,你都应该强制再次进行IOS 5的自动旋转,但现在你将使用
UIDeviceOrientationPortrait
,因为你进入了一个PORTRAIT控制器:
- (void)viewDidLoad{
[super viewDidLoad];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0)
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO];
}
现在最后一件事情(有点奇怪)——您必须根据IOS更改从一个控制器切换到另一个控制器的方式:
创建一个NSObject类“Schalter”(德语中的“Switch”)。
在Schalter.h文件中添加以下内容:
#import <Foundation/Foundation.h>
@interface Schalter : NSObject
+ (void)loadController:(UIViewController*)VControllerToLoad andRelease:(UIViewController*)VControllerToRelease;
@end
在Schalter.m中说:
#import "Schalter.h"
#import "AppDelegate.h"
@implementation Schalter
+ (void)loadController:(UIViewController*)VControllerToLoad andRelease:(UIViewController*)VControllerToRelease{
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;
if (IOS_OLDER_THAN_6)
[((AppDelegate*)[UIApplication sharedApplication].delegate).window addSubview:VControllerToLoad.view];
else
[((AppDelegate*)[UIApplication sharedApplication].delegate).window setRootViewController:VControllerToLoad];
[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。