我给了一个应用,比如说10个视图控制器.我使用导航控制器来加载/卸载它们.

除了一个以外,其他都处于纵向模式.假设第七个VC在风景区.我需要它在加载时呈现在景观中.

Please suggest a way to force the orientation go from portrait to landscape in 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被打了电话.

What I have have tried in IOS 6:

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

加载时,控制器保持纵向.旋转设备后,方向变化正常.但我需要让控制器在加载时自动旋转到横向,因此用户必须旋转设备才能正确查看数据.

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

此外,上述3种方法中有NONE种被调用.

Some (useful) data:

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

感谢您的帮助.谢谢

推荐答案

好的,各位,我会发布我的解决方案.

我所拥有的:

  1. 基于视图的应用程序,具有多个视图控制器.(它是基于导航的,但由于方位问题,我不得不让它基于视图).
  2. 所有视图控制器都是纵向的,只有一个除外-景观左.

任务:

  1. 我的一个视图控制器必须自动旋转到横向,无论用户如何握住设备.所有其他控制器都必须是纵向的,离开横向控制器后,应用程序必须强制旋转到纵向,无论用户如何握住设备.
  2. 这必须在IOS 6上运行.IOS 5上的x.十、

go

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

In all your PORTRAIT view controllers override autorotation methods like this:

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

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

The same for your LANDSCAPE view controller, with some additions and changes:

- (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;
}

ATTENTION:要在IOS 5中强制自动旋转,应添加以下内容:

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

类似地,在离开横向控制器后,无论加载什么控制器,都应该再次强制IOS 5自动旋转,但现在,当您转到纵向控制器时,将使用UIDeviceOrientationPortrait:

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

Now the last thing (and it's a bit weird) - you have to change the way you switch from a controller to another, depending on the IOS:

制作一个NSObject类"Schalter"("Switch"来自德语).

在Schalter.h说:

#import <Foundation/Foundation.h>

@interface Schalter : NSObject
+ (void)loadController:(UIViewController*)VControllerToLoad andRelease:(UIViewController*)VControllerToRelease;
@end

在Schalter.我说:

#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

NOW, this is the way you use Schalter ( suppose you go from Warehouse controller to 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];
}

Well, this is it. Don't hesitate to downvote, I don't care. This is for the ones who are looking for solutions, not for reputation. Cheers! Sava Mazare.

Objective-c相关问答推荐

UITableViewCell:如何防止蓝色 Select ​​背景不带 isSelected 属性?

iPhone SDK 网络连接检测

如何在没有 Interface Builder 的情况下创建 Cocoa 接口?

多个 WKWebView 之间的 Cookie 共享

如何删除警告按钮的框架在运行时会有所不同.

使用 ARC,什么更好:alloc 或 autorelease 初始化程序?

ScrollView 手势识别器吃掉所有的事件

Container View 好像有 UINavigationBar 一样被下推?

iOS 6 Facebook 发布过程以remote_app_id 与存储的 id 不匹配结束

从 UIScrollView 中删除所有子视图?

UILabel 的角半径属性在 iOS 7.1 中不起作用

Objective-C 中是否有一些文字字典或数组语法?

如何保存我的 iPhone 应用程序的用户首选项?

如何在Objective C(NSRegularExpression)中编写正则表达式?

我应该使用 NSUserDefaults 还是 plist 来存储数据?

关闭一次后如何返回允许推送通知对话框?

iOS Buttons - 添加边框

Core Text在iOS中计算字母框架

如何本地化 iOS 故事板

判断文件是否存在于路径中