When are views automatically transformed based on their superview?
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>>"
)
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
}
精彩评论