请不要建议我在 1 个版本中轮换 ios 4.3-6.0 是一个坏主意,因为我告诉过很多次但没有听我的。
在项目设置中,我已将所有界面方向设置为受支持,以确保。
现在我正在用 iphone4 在 ios6 上进行测试。从 app delegate 开始,它是一个代码,如下所示:
mainController = [[MainViewController alloc] initWithNibName:nil bundle:nil];
navigationController=[[RotatingNavigationController alloc] initWithRootViewController:mainController];
navigationController.navigationBar.hidden =true;
// ios: 4 and 5
//[window addSubview:navigationController.view];
//ios6:
window.rootViewController = navigationController;
[window makeKeyAndVisible];
所以我做了2个自定义类,在可能的情况下推荐用于自动旋转,并且为他们解决了问题。
RotatingNavigationController 是一个自定义UINavigationController
的,代码有点难看,但是我从这个论坛收集了这些部分,所以应该允许发布它:
@implementation RotatingNavigationController
- (id)init {
self = [super init];
if (self) {
self.view.autoresizesSubviews = YES;
[self.view setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
}
return self;
}
// ios6 require this method for rotation detection, available from ios5, ios4.3 not available: sux
//- (void)viewWillLayoutSubviews
//{
//UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation;
//[[NSNotificationCenter defaultCenter] postNotificationName:UIDeviceOrientationDidChangeNotification object:nil];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
/*
if(UIInterfaceOrientationIsLandscape(statusBarOrientation)){
[self willRotateToInterfaceOrientation: statusBarOrientation duration: 0.3 ];
}
else{
[self willRotateToInterfaceOrientation: statusBarOrientation duration: 0.3 ];
}
*/
//}
//ios 4.3 and ios 5.0, at ios6 it isn't called by iOS...sux
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[self.visibleViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
// //ios 4.3 and ios 5.0, at ios6 need different methods, which sux
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
//Autorotation is changing in iOS 6. In iOS 6, the shouldAutorotateToInterfaceOrientation: method of UIViewController is deprecated. In its place, you should use the supportedInterfaceOrientationsForWindow: and shouldAutorotate methods.
// ios6
- (NSUInteger)supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskAll;
}
// ios6
- (BOOL)shouldAutorotate
{
return YES;
}
// ios6
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationMaskPortrait;
}
@end
- 这应该会正确生成自动旋转通知,我认为他完成了他的工作。
这MainViewController
是一种习惯UIViewController
。
只是为了确保我已经复制粘贴了代码:
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
// return (interfaceOrientation == UIInterfaceOrientationPortrait);
return YES;
}
// ios6:
- (NSUInteger)supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskAll;
}
// ios6
- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationMaskPortrait;
}
他的工作是根据设备状态(旋转)更改不同的 UIViewController
我在某个地方有一个菜单屏幕,它有不同的 xib,但也有 h 和 m 文件(因为有不同的元素,不会出现在 Portait 或 Landscape 中。(我没有能力改变整个架构)
代码的一部分 - 这里应该有一些问题,我认为在这个主控制器下面:
#pragma mark
#pragma mark Rotation
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(deviceOrientation == UIDeviceOrientationFaceUp || deviceOrientation == UIDeviceOrientationFaceDown){
if(debug){
NSLog(@"Nothing to change because it is gone to Flat");
}
return;
}
NSArray *viewControllers = [self.navigationController viewControllers];
if (UIDeviceOrientationIsLandscape(deviceOrientation)){
//if(debug)
// NSLog(@"Portait -> Landscape size : %3.0fw, %3.0fh", self.view.bounds.size.width, self.view.bounds.size.height);
id lastviewController = viewControllers.lastObject;
// check what is there:
if([lastviewController isKindOfClass:[MenuController class]]){
[self.navigationController popViewControllerAnimated:NO];
[self.navigationController pushViewController:menuLandscape animated:NO];
NSLog(@"poped Menu Portrait, pushed Menu Landscape");
}
...
else if(UIDeviceOrientationIsPortrait(deviceOrientation)){
//else if(UIInterfaceOrientationIsLandscape(statusBarOrientation)){
//if(debug)
// NSLog(@"Landscape -> Portait , size : %3.0fw, %3.0fh", self.view.bounds.size.width, self.view.bounds.size.height);
id lastviewController = viewControllers.lastObject;
// check what is there:
if([lastviewController isKindOfClass:[MenuLandscapeController class]]){
[self.navigationController popViewControllerAnimated:NO];
[self.navigationController pushViewController:menuPortait animated:NO];
}
...
问题是:景观被取出并被推到肖像上,当它倒置时,但那个肖像没有旋转并且它显示出一个破碎的布局。
我怎样才能唤醒那个控制器并告诉他该旋转了,因为它不是纵向模式?
感谢与我关于架构更改之外的任何改进的问题相关的任何建议。
更新:
在 app delgate 没有帮助添加:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskAll;
}
其他答案的代码:在此处介绍
更新2:
UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation;
由于某种原因不想UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown
取值,我认为需要解决。
Update3:非常仔细地阅读了两次和第三次ios6 发行说明
系统通过将应用程序的supportedInterfaceOrientationsForWindow: 方法返回的值与最顶部全屏控制器的supportedInterfaceOrientations 方法返回的值相交来确定是否支持方向。
- 现在再读一遍:)