【iOS界面开发】UIWindow屏幕旋转自适应
程序员文章站
2022-06-23 12:17:36
【iOS界面开发】UIWindow屏幕旋转自适应。屏幕上显示的每个视图都被包含于UIWindow对象,app内每个window之间相互独立。app接收到的事件都最先路由到合适的wi...
【iOS界面开发】UIWindow屏幕旋转自适应。屏幕上显示的每个视图都被包含于UIWindow对象,app内每个window之间相互独立。app接收到的事件都最先路由到合适的window对象,再由此派发到合适的视图。
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 */