【iOS界面开发】UIWindow屏幕旋转自适应

屏幕上显示的每个视图都被包含于UIWindow对象,app内每个window之间相互独立。app接收到的事件都最先路由到合适的window对象,再由此派发到合适的视图。需要注意的是,keyWindow接收键盘事件和其他与触摸无关的事件。

Windows与ViewControllers一起协作,从而实现屏幕旋转变换,完成各项任务。避免直接调用- [UIWindow addSubview:]方法来添加视图,而是通过操作UIViewController来添加视图,从而实现屏幕旋转自适应。

屏幕上的window根据是否是keyWindow,可分成两种:

  • keyWindow:app必需,且同时间只会有一个。

例子:

UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.rootViewController = [[UIViewController alloc] init];
[window makeKeyAndVisible];
/** keep strong reference to window */
  • 其他辅助window:用于显示alert/statusBar等辅助视图。

例子:

UIWindow *window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 64)];
window.windowLevel = UIWindowLevelStatusBar + 1;
/** keep strong reference to window */
CustomViewController *vc = [[CustomViewController alloc] init];
/** CustomViewController could rewite shouldAutorotate & supportedInterfaceOrientations to support orientations  */
window.rootViewController = vc;
window.hidden = NO; /** must have to show */

猜你喜欢

转载自blog.csdn.net/freeWayWalker/article/details/78013967
今日推荐