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
4

3 回答 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];

编辑

#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
于 2013-03-04T19:43:14.853 回答
1

尝试添加:

_window.rootViewController = _viewController;

编辑:无论如何,您的代码看起来像使用“已弃用”样式。我建议为 iOS6 建立一个新项目并使用它的模板。它在 iOS5 上运行,但 iOS 发生了变化。

于 2013-03-04T20:14:55.450 回答
0

只有rootViewController您的UIWindow人会收到轮换事件。

[_window setRootViewController:_rootViewController];

查看苹果技术说明

于 2013-03-04T20:17:07.553 回答