我正在开发一个代码,其中初始视图控制器是视图landscape
。按下该视图控制器中的按钮时,应portrait
下一个视图控制器。我的root view controller
是navigation controller
.
这是一个故事板应用程序。
我在第一个根视图控制器中返回了 Yes for UIInterfaceOrientationLandscapeRight
和 UIInterfaceOrientationPortrait
第二个视图控制器。但是轮换一开始不起作用。我们必须旋转视图以将其更改为正确的旋转。
如何解决此问题?
设备方向的一种方法是......
-(void)viewWillAppear:(BOOL)animated
{
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}
在这里你想在哪个类中改变方向,然后在你的viewWillAppear:
方法中写下这行
并且还要删除UIDevice的警告,只需在.m文件中声明以下代码
@interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
@end
这里这段代码在@implementation文件上面定义希望这对您有所帮助...
:)
加载第二个ViewController
时,在viewDidLoad
或方向更改事件接收方法中添加以下行:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
编辑:
好的,然后在要纵向显示的CiewController中更新您的方法shouldAutorotateToInterfaceOrientation
,如下所示:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}
还要确保在 nib 文件中,您已在属性检查器中将方向设置为Portrait
。
它将为您完成工作。