如何设置第一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)

该子视图正确调整大小,但父视图仍然具有面向肖像的框架。

如何使顶层视图调整大小以适合横向模式?

--------------解决方案-------------

尝试这个:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {

//is portrait

view.frame = CGRectMake(0,0,320,480)

} else {

//is landscape

view.frame = CGRectMake(0,0,480,320);

}
}

希望这可以帮助!

在你的viewController使用此

self.view.frame = [[UIScreen mainScreen] bounds];

你必须允许窗口自动调整大小子视图:

window.autoresizesSubviews=YES;

希望这可以帮助!

分类:IOS 时间:2012-04-12 人气:0
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.434 (s). 10 q(s)