我有一個UIViewController
子類來處理我的景觀唯一的應用程序的視圖。我希望自己調整大小以自動反映景觀尺寸,但似乎並非如此。子視圖,但是,。如何將第一個UIViewController的視圖設置爲自動大小?
這是再現代碼。
@interface MyViewController : UIViewController {
UIView *subview;
}
@implementation MyViewController
- (id)init
{
self = [super init];
if (self) {
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
subview = [[UIView alloc] initWithFrame:self.view.frame];
subview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:subview];
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
NSLog(@"My view's frame (%f,%f)", self.view.frame.size.width, self.view.frame.size.height);
NSLog(@"Subview's frame (%f,%f)", subview.frame.size.width, subview.frame.size.height);
}
@end
然後......
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.window.rootViewController = [[MyViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
超級簡單。然而,在啓動時,它會記錄:
My view's frame (320.000000,480.000000)
Subview's frame (480.000000,320.000000)
子視圖的大小調整正確,但父視圖仍然有一個縱向框架。
如何使頂層視圖的大小適合橫向模式?
不幸的是,它只是硬編碼,這是微不足道的,但我想避免。即使在當前的設備生態系統中,代碼也需要另一個分支來檢查設備慣用語,手機或鍵盤。 –