我有一个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)
子视图正确调整大小,但父视图仍然具有纵向框架。
如何调整顶层视图以适应横向模式?
答案 0 :(得分:2)
在viewController中使用它
self.view.frame = [[UIScreen mainScreen] bounds];
答案 1 :(得分:2)
您必须允许窗口自动调整子视图:
window.autoresizesSubviews=YES;
希望这有帮助!
答案 2 :(得分:1)
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
在viewdidload中添加此内容。确保在storyboard中,Viewcontrollers视图属性被分配给视图
答案 3 :(得分:0)
试试这个:
-(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);
}
}
希望这有帮助!