ary*_*axt 27 iphone objective-c uiwindow screen-orientation
我正在为我的应用添加一个额外的UIWindow.我的主窗口正确旋转,但我添加的这个附加窗口不会旋转.
根据当前设备方向旋转UIWindow的最佳方法是什么?
Mor*_*ast 45
你需要为UIWindow自己动手.
侦听UIApplicationDidChangeStatusBarFrameNotification
通知,然后在状态栏更改时设置转换.
您可以从中读取当前方向-[UIApplication statusBarOrientation]
,并像这样计算变换:
#define DegreesToRadians(degrees) (degrees * M_PI / 180)
- (CGAffineTransform)transformForOrientation:(UIInterfaceOrientation)orientation {
switch (orientation) {
case UIInterfaceOrientationLandscapeLeft:
return CGAffineTransformMakeRotation(-DegreesToRadians(90));
case UIInterfaceOrientationLandscapeRight:
return CGAffineTransformMakeRotation(DegreesToRadians(90));
case UIInterfaceOrientationPortraitUpsideDown:
return CGAffineTransformMakeRotation(DegreesToRadians(180));
case UIInterfaceOrientationPortrait:
default:
return CGAffineTransformMakeRotation(DegreesToRadians(0));
}
}
- (void)statusBarDidChangeFrame:(NSNotification *)notification {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
[self setTransform:[self transformForOrientation:orientation]];
}
Run Code Online (Sandbox Code Playgroud)
根据您的窗口大小,您可能还需要更新框架.
Mec*_*cki 10
只需创建一个UIViewController
自己的UIView
,将其分配rootViewController
给您的窗口,并将所有进一步的UI添加到控制器的视图(而不是直接添加到窗口),控制器将为您处理所有轮换:
UIApplication * app = [UIApplication sharedApplication];
UIWindow * appWindow = app.delegate.window;
UIWindow * newWindow = [[UIWindow alloc] initWithFrame:appWindow.frame];
UIView * newView = [[UIView alloc] initWithFrame:appWindow.frame];
UIViewController * viewctrl = [[UIViewController alloc] init];
viewctrl.view = newView;
newWindow.rootViewController = viewctrl;
// Now add all your UI elements to newView, not newWindow.
// viewctrl takes care of all device rotations for you.
[newWindow makeKeyAndVisible];
// Or just newWindow.hidden = NO if it shall not become key
Run Code Online (Sandbox Code Playgroud)
当然,也可以在界面构建器中使用单行代码创建完全相同的设置(除了在显示窗口之前设置帧大小以填充整个屏幕).
归档时间: |
|
查看次数: |
16234 次 |
最近记录: |