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

发布于 2024-11-27 06:36:02 字数 2917 浏览 3 评论 0原文

我之前问过类似的问题,但现在我做了更多的工作,并提出了一个更精致的问题。

我的 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 度。 (就好像设备仍处于纵向状态一样)。

真正让我困惑的是,第一个和第二个视图看起来还不错。如果我取出添加不可见视图的中间块,那么 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>>"
)

I asked a similar question before, but now I have done some more work on it and have a more refined question.

I have the following code in my applicationDidFinishLaunching:, creating 3 views and adding them as subviews of the window:

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

The app is supposed to work only in landscape, so I have set it (in the plist) to launch in landscape, which it does. But the last added subview (GazeIntroViewController) appears rotated by 90 deg. (as if the device was still in portrait).

What's really baffling me, is that the first and second view seem ok. If I take out the middle block where I add the invisibleView, then GazeIntroViewController will also come out right. From the NSLogs, I can see that the views that come out right have a transform automatically applied ([0, 1, -1, 0, 0, 0]), but I don't understand why this transform is applied to some views and not others, when their parent is the window for all of them.

Output when invisibleView is not added:

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>>"
)

Output when invisibleView is added:

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>>"
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行至春深 2024-12-04 06:36:02

所有应用程序仅以纵向模式启动,然后在 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
}

All apps starts with portrait mode only and then in UIViewController's autoRotate methode it turns to landscape.

so, we cant change orientation of MainWindow.xib's orientation. thats why if you will add a view as subView of main window it will always in portrait mode and you think that it is rotated at 90 degree.

try to add sub view on rootViewController.view like:

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

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

and if you are using navigation controller then use this:;

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

    // Some code    
     [self.navigationController.topViewController.view addSubview:YourView];
    //some code
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文