gpt4 book ai didi

iphone - IOS 6强制设备方向为横向

转载 作者:IT老高 更新时间:2023-10-28 11:30:35 28 4
gpt4 key购买 nike

我给了一个带有 10 个 View Controller 的应用程序。我使用导航 Controller 来加载/卸载它们。

除了一个以外,其他所有人都处于纵向模式。假设第 7 个 VC 在横向。我需要它在加载时以横向呈现。

请建议一种在 IOS 6 中强制将方向从纵向变为横向的方法(在 IOS 5 中也可以使用)。

这是我在 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 被调用。

我在 IOS 6 中的尝试:

- (BOOL)shouldAutorotate{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeLeft;
}

加载时, Controller 保持纵向。旋转设备后,方向改变就好了。但我需要让 Controller 在加载时自动旋转为横向,因此用户必须旋转设备才能正确查看数据。

另一个问题:将设备旋转回纵向后,方向变为纵向,尽管我在 supportedInterfaceOrientations 中仅指定了 UIInterfaceOrientationMaskLandscape。为什么会这样?

此外,上述 3 个方法中的 NONE 被调用。

一些(有用的)数据:

  1. 在我的 plist 文件中,我指定了 3 个方向 - 几乎都是颠倒的。
  2. 该项目是在 Xcode 4.3 IOS 5 中启动的。包括 xibs 在内的所有类都是在 Xcode 4.5 IOS 6 之前创建的,现在我使用最后一个版本。
  3. 在 plist 文件中,状态栏设置为可见。
  4. 在 xib 文件(我想要的横向)中,状态栏为“无”,方向设置为横向。

感谢任何帮助。谢谢。

最佳答案

好的,伙计们,我会发布我的解决方案。

我有什么:

  1. 基于 View 的应用程序,具有多个 View Controller 。 (它是基于导航的,但由于方向问题,我不得不让它基于 View )。
  2. 所有 View Controller 都是纵向的,除了一个 - LandscapeLeft。

任务:

  1. 无论用户如何握住设备,我的一个 View Controller 都必须自动旋转为横向。所有其他 Controller 都必须是纵向的,并且在离开横向 Controller 后,无论用户如何握住设备,应用都必须强制旋转到纵向。
  2. 这必须像在 IOS 6.x 上一样在 IOS 5.x 上工作

去吧!

(更新删除了@Ivan Vučica 建议的宏)

在您的所有 PORTRAIT View Controller 中,都会像这样覆盖自动旋转方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}

您可以看到两种方法:一种适用于 IOS 5,另一种适用于 IOS 6。

与您的 LANDSCAPE View Controller 相同,但有一些添加和更改:

- (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 Controller 后,无论您加载什么 Controller ,您都应该再次强制 IOS 5 自动旋转,但现在您将使用 UIDeviceOrientationPortrait,因为您转到 PORTRAIT Controller :

- (void)viewDidLoad{
[super viewDidLoad];
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 6.0)
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO];
}

现在最后一件事(这有点奇怪)——你必须改变从一个 Controller 切换到另一个 Controller 的方式,这取决于 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{

//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 Controller 转到 Products Controller ):

#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];
}

嗯,就是这样。不要犹豫投反对票,我不在乎。这是给那些正在寻找解决方案的人,而不是为了声誉。干杯!萨瓦马扎雷。

关于iphone - IOS 6强制设备方向为横向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12640870/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com