我有一个简单的 IPAD 结构,其中包含一个来自 viewController 的 View 的 AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ClockVC *clockVC = [[ClockVC alloc]init];
clockVC.view.frame = CGRectMake(100, 100, clockVC.view.bounds.size.width, clockVC.view.bounds.size.height);
[self.window addSubview:clockVC.view];
[self.window makeKeyAndVisible];
return YES;
}
clockVC 有一个由这段代码定义的 viewDidLoad :
- (void)viewDidLoad {
[super viewDidLoad];
self.view.autoresizingMask = UIViewAutoresizingNone;
self.view.autoresizesSubviews = UIViewAutoresizingNone;
}
clockVC 的边界由 IB 定义并在 application:didFinishLaunching 中覆盖... 宽度和高度分别为 200 和 150。
ClockVC实现一步自动旋转的方法: /
/ Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[UIView animateWithDuration:0.5
animations:^{self.view.alpha = 0;}
];
}
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
[UIView animateWithDuration:1
animations:^{self.view.alpha = 1;}
];
}
在第一次加载时,页面被正确查看并且 clockVC View 就位。 当我旋转 clockVC View (其 autoResizeMask 设置为 UIViewAutoresizingNon)时调整大小以占用整个屏幕。
为什么?我想保持初始大小。
这里是我的问题的截图。 旋转前:
旋转后:
最佳答案
不要为此使用 Root View ,而是将 subview 添加到 View Controller 的 View 中。 UIViewControllers
通常设计为填充其容器的整个空间(在本例中为窗口)。
例如,如果您要将 UIViewController
添加到 UINavigationController
,导航 Controller 将接管根据导航 Controller 的布局调整 View Controller View 大小的责任(导航栏是否可见等)。我不确定 UIWindow 的行为究竟如何(我认为这不是很清楚地记录),但在 SDK 的很多地方, View Controller 的父级负责其子级的定位和大小(UIPopoverController
, UITabBarController
等)。
关于objective-c - UIViewAutoresizingNone : Resize after rotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5804431/