我正在将一个额外的UIWindow添加到我的应用程序中。
我的主窗口可以正确旋转,但是我添加的这个额外窗口却无法进行旋转。
如何根据当前设备方向旋转UIWindow是最佳方式?
我正在将一个额外的UIWindow添加到我的应用程序中。
我的主窗口可以正确旋转,但是我添加的这个额外窗口却无法进行旋转。
如何根据当前设备方向旋转UIWindow是最佳方式?
#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]];
}
根据您的窗口大小,您可能需要更新框架。
x
变成了 y
,width
变成了 height
吗?还是没有变化? - oleynikdUIWindow
处理方向更改而不需要旋转...那么还有其他方法吗?有人知道吗? - oleynikd只需创建一个带有自己 UIView
的 UIViewController
,将其分配为您的窗口的 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
当然,可以在界面构建器中创建完全相同的设置,而不需要编写任何代码(除了在显示窗口之前将框架大小设置为填充整个屏幕)。viewctrl
)执行的,而且这个ViewController只旋转它控制的视图(即newView
),然后递归地旋转所有子视图。如果您直接将它们添加到newWindow
中,则您的视图不会被ViewController旋转,您将不得不手动再次旋转它们(当您旋转设备时,UIWindow不会旋转任何视图,甚至不会旋转自身)。 - MeckimyNewWindow!.rootViewController = self
然后你可以在旋转方法中改变框架。
例如(iOS8中的Swift)
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
customAlertView.frame = UIScreen.mainScreen().bounds
}
我不知道你是否对窗口进行任何操作。但是根控制器需要以YES响应shouldAutorotate。
fileprivate(set) var bottonOverlayWindow = UIWindow()
self.bottonOverlayWindow.rootViewController = self;
// 'self'表示您已添加UIWindow视图的ViewController。因此,每当您的ViewController更改方向时,窗口视图也会更改其方向。
如果遇到任何问题,请告诉我。