iOS 应用程序中的 UIView 旋转,内容未显示

3

我正在开发一个iOS应用程序,它只支持横向左右屏幕方向。我已经通过多种方式进行了指定:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation); //iOS 5 compatibility.
}
-(NSInteger)supportedInterfaceOrientations:(UIWindow *)window{
    return UIInterfaceOrientationMaskLandscape; //iOS 6
}
-(BOOL)shouldAutorotate{
    return YES;
}

我已在应用的Info.plist中将支持的方向指定为仅横屏。当我在设备上运行应用程序(甚至在模拟器中),我知道根视图处于横向方向(切换器位于屏幕的长边上,通知中心也是如此),但是内容仍然从屏幕左上角开始,就像是纵向模式,而不是我想要的左下角或右上角。我不知道是什么原因导致内容无法随父视图旋转,因为这段代码在iOS 5上可以工作,但在iOS 6上却不能。

编辑:这里有一张截图。

编辑:这是我的main.m文件:

#import <UIKit/UIKit.h>
int main(int argc, char **argv) {
    NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
    int ret = UIApplicationMain(argc, argv, @"ClockApplication", @"ClockApplication");
    [p drain];
    return ret;
}

这是我的ClockApplication.mm文件:

#import "RootViewController.h"

@interface ClockApplication: UIApplication <UIApplicationDelegate> {
    UIWindow *_window;
    RootViewController *_viewController;
}
@property (nonatomic, retain) UIWindow *window;
@end

@implementation ClockApplication
@synthesize window = _window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _viewController = [[RootViewController alloc] init];
    [_window addSubview:_viewController.view];
    [_window makeKeyAndVisible];
}

- (void)dealloc {
    [_viewController release];
    [_window release];
    [super dealloc];
}
@end

我不是很清楚你想要如何呈现... 如果可能的话,请发布一些图片,甚至线框图... - CainaSouza
你正在使用导航控制器吗? - CainaSouza
你尝试过旋转设备吗?也许由于某种原因它以纵向模式启动。 - Michal Pietras
@MichalPietras 是的,我已经旋转了设备,但是什么也没有旋转。我应该提到这一点。 - Aehmlo
@MichalPietras 它返回了4,这是左横屏。 - Aehmlo
显示剩余5条评论
3个回答

1
尝试将您的方法从这个改为:
-(NSInteger)supportedInterfaceOrientations:(UIWindow *)window{
}

到这个

-(NSUInteger)supportedInterfaceOrientations{
}

编辑

尝试像这样在导航控制器中调用您的视图:

UIViewController *yourViewController = [[UIViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:yourViewController];
nc.navigationBarHidden = YES;
[self.navigationController presentModalViewController:nc animated:NO];
[self.navigationController dismissModalViewControllerAnimated:NO];

EDIT

#import "RootViewController.h"

@interface ClockApplication: UIApplication <UIApplicationDelegate> {
    UIWindow *_window;
    RootViewController *_viewController;
}
@property (nonatomic, retain) UIWindow *window;
@end

@implementation ClockApplication
@synthesize window = _window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:_viewController];
    nc.navigationBarHidden = YES;

    _window.rootViewController = nc;
    [_window makeKeyAndVisible];
}

- (void)dealloc {
    [_viewController release];
    [_window release];
    [super dealloc];
}
@end

还有,设置一个断点在这个方法(带有这个新签名的新方法),并检查它是否正在被调用。 - CainaSouza
它触发了断点,所以正在被调用。但问题仍然存在。 - Aehmlo
刚才我在答案中提供的内容试一下。 - CainaSouza
ClockApplication 没有 navigationController 属性。 - Aehmlo
那么 self.navigationController 是从哪里来的?它不是继承而来的,对吗? - Aehmlo
显示剩余5条评论

1

Try to add:

_window.rootViewController = _viewController;

编辑:无论如何,你的代码看起来像是使用了“废弃”的风格。我建议为iOS6设置一个新项目并使用其模板。它在iOS5上工作,但iOS已经改变了。


0

只有你的 UIWindowrootViewController 才会接收旋转事件。

[_window setRootViewController:_rootViewController];

请查看苹果技术说明


谢谢,这似乎是解决我的问题最简单的方法。有没有想法为什么在iOS 5上不是这种情况(例如,在iOS 5上它运行良好,但在6上不行)? - Aehmlo
iOS 6需要rootViewController。 - CainaSouza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接