I'm creating a puzzle game for the iphone. All of my menus, etc are only in the portrait orientation, but my gameplay can be in either portrait or landscape.
我正在为iphone开发一款益智游戏。我所有的菜单,等等都是在肖像朝向,但我的游戏可以在肖像或风景。
So when I'm in the menu and the device is being held in landscape, then when I go to the gameplay viewcontroller, it does the layout with the landscape coordinates, but the screen is still actually oriented portrait-wise:
所以当我在菜单中设备被放在横向视图控制器中,当我去gameplay视图控制器时,它用横向坐标做布局,但是屏幕仍然是面向纵向的
I want to force the device to re-orient the screen before I do the layout. How do I do that?
在进行布局之前,我想强制设备重新定位屏幕。我该怎么做呢?
right now I'm testing for device orientation like this:
现在我正在测试设备朝向,像这样:
- (void) viewDidLoad {
.
.
.
if (!UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {
// Run Portrait Layout
}
else {
// Run Landscape Layout
}
}
So I know that the device is returning an orientation of landscape, but the screen is still oriented portrait for some reason.
所以我知道这个设备返回的是一个朝向,但是由于某种原因屏幕仍然是朝向的人像。
2 个解决方案
#1
17
Perhaps try..
也许试试. .
[UIViewController attemptRotationToDeviceOrientation];
In viewDidLoad
or viewWillAppear
, that should make it attempt an orientation change
在viewDidLoad或viewWillAppear中,这会使它尝试方向更改
#2
1
There are a few methods in UIViewController that you can override to catch the view-did-rotate event:
在UIViewController中有一些方法可以重写以捕获视图-did-rotate事件:
– willRotateToInterfaceOrientation:duration:
– willAnimateRotationToInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:
Do check the UIViewController class reference
检查UIViewController类引用吗?
#1
17
Perhaps try..
也许试试. .
[UIViewController attemptRotationToDeviceOrientation];
In viewDidLoad
or viewWillAppear
, that should make it attempt an orientation change
在viewDidLoad或viewWillAppear中,这会使它尝试方向更改
#2
1
There are a few methods in UIViewController that you can override to catch the view-did-rotate event:
在UIViewController中有一些方法可以重写以捕获视图-did-rotate事件:
– willRotateToInterfaceOrientation:duration:
– willAnimateRotationToInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:
Do check the UIViewController class reference
检查UIViewController类引用吗?