视图何时根据其超级视图自动转换?

时间:2011-08-01 04:16:25

标签: ios objective-c ipad uiview autoresizingmask

之前我问了一个类似的问题,但现在我已经做了一些更多的工作并且有一个更精确的问题。

我在applicationDidFinishLaunching中有以下代码:创建3个视图并将它们添加为窗口的子视图:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    GLViewController* controller = [[GLViewController alloc] init];
    self.glViewController = controller;
    [controller release];

    glView = [[GLView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    glView.controller = glViewController;
    [glView stopAnimation];
    [window addSubview:glView];

    invisibleView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    invisibleView.alpha = 1.0;
    invisibleView.tag = 2;
    // By the definition of UIViewAutoresizing, 0x3F corresponds to resizing in every direction.
    invisibleView.autoresizingMask = 0x3F;
    obstaclesViewController = [[ObstaclesViewController alloc] init];
    obstaclesViewController.view = invisibleView;
    [window addSubview:invisibleView];

    viewController = [[GazeIntroViewController alloc] initWithNibName:@"GazeIntroViewController" bundle:[NSBundle mainBundle]];
    [viewController.view setFrame:[[UIScreen mainScreen] bounds]];
    [window addSubview:viewController.view];
    [window bringSubviewToFront:viewController.view];

    [window makeKeyAndVisible];

    NSLog(@"window: %@", [window description]);
    NSLog(@"window subviews: %@", [[window subviews] description]);
}

应用程序应该只在横向上工作,所以我已经设置它(在plist中)以横向启动它。但最后添加的子视图(GazeIntroViewController)显示旋转了90度。 (好像设备仍处于人像状态)。

令我感到困惑的是,第一和第二种观点看起来还不错。如果我取出中间块,我添加了invisibleView,那么GazeIntroViewController也会出现。从NSLogs,我可以看到右边的视图有一个自动应用的变换([0,1,-1,0,0,0]),但是我不明白为什么要应用这个变换当他们的父母是他们所有人的窗口时,有些观点而不是其他观点

未添加invisibleView时的输出:

2011-07-31 21:00:51.271 GazeDemo[53515:207] window: <UIWindow: 0xa713ab0; frame = (0 0; 768 1024); autoresize = RM+BM; layer = <CALayer: 0xa713b90>>
2011-07-31 21:00:51.277 GazeDemo[53515:207] window subviews: (
"<GLView: 0x68bc350; frame = (0 0; 768 1024); layer = <CAEAGLLayer: 0x68bc4e0>>",
"<UIView: 0x68c3ce0; frame = (0 0; 748 1024); transform = [0, 1, -1, 0, 0, 0]; autoresize = LM+W+RM+TM+H+BM; layer = <CALayer: 0x68c2150>>"
)

添加invisibleView时的输出:

2011-07-31 21:11:05.489 GazeDemo[53710:207] window: <UIWindow: 0x689d4d0; frame = (0 0; 768 1024); autoresize = RM+BM; layer = <CALayer: 0x689d5b0>>
2011-07-31 21:11:05.496 GazeDemo[53710:207] window subviews: (
"<GLView: 0x689cf50; frame = (0 0; 768 1024); layer = <CAEAGLLayer: 0x689dcb0>>",
"<UIView: 0x68a05f0; frame = (0 0; 748 1024); transform = [0, 1, -1, 0, 0, 0]; autoresize = LM+W+RM+TM+H+BM; tag = 2; layer = <CALayer: 0x68a06c0>>",
"<UIView: 0x6b484b0; frame = (0 0; 768 1024); autoresize = LM+W+RM+TM+H+BM; layer = <CALayer: 0x6b4bad0>>"
)

1 个答案:

答案 0 :(得分:0)

所有应用程序仅以纵向模式启动,然后在UIViewController的autoRotate方法中转为横向。

所以,我们无法改变MainWindow.xib方向的方向。这就是为什么如果你将视图添加为主窗口的子视图,它将始终处于纵向模式,你认为它旋转90度。

尝试在rootViewController.view上添加子视图,如:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    // Some code    
     [self.window.rootViewController.view addSubview:YourView];
    //some code
}

如果您使用的是导航控制器,请使用:;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    // Some code    
     [self.navigationController.topViewController.view addSubview:YourView];
    //some code
}