强制 iOS 设备改变方向
Force the iOS device to change orientation
首先,我很抱歉再次问同样的问题,这个问题已经在这个论坛问了很多次了。但是,我的问题是我已经尝试了所有建议的解决方案,但仍然没有解决我的问题。
我有一个 ViewControllerA
的肖像模式和 ViewControllerB
的横向模式。当设备从纵向变为横向时,我可以看到 ViewControllerB
以横向模式打开,但是当设备旋转回纵向模式时,显示 ViewControllerA
但仍处于横向模式。谁能帮助我了解如何以肖像模式显示它?
我尝试了以下方法,但对我没有任何作用:
在 viewWillAppear
中的 ViewControllerA
中添加了以下代码:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
在此视图控制器中还添加了以下功能:
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
请尝试
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
希望这可以帮助您使用 supportedInterfaceOrientationsForWindow
Appdelegate.h
@property () BOOL restrictRotation;
在Appdelegate.m
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
//restrictRotation is BOOL value
if(self.restrictRotation)//set the bool value to view controller which you want to load in landscape
return UIInterfaceOrientationMaskLandscape;
else
return UIInterfaceOrientationMaskAll;
}
//在你需要的地方调用这个
requiredViewController.m
-(void) restrictRotation:(BOOL) restriction
{
appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
//在ViewdidLoad()
- (void)viewDidLoad
{
[super viewDidLoad];
[self restrictRotation:YES];//Set YES if required in Landscape mode
}
使用下面的代码
-(void) viewDidAppear:(BOOL)animated
{
[UIViewController attemptRotationToDeviceOrientation];
[super viewDidAppear:animated];
}
&也在viewWIllAppear写
[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"]
首先,我很抱歉再次问同样的问题,这个问题已经在这个论坛问了很多次了。但是,我的问题是我已经尝试了所有建议的解决方案,但仍然没有解决我的问题。
我有一个 ViewControllerA
的肖像模式和 ViewControllerB
的横向模式。当设备从纵向变为横向时,我可以看到 ViewControllerB
以横向模式打开,但是当设备旋转回纵向模式时,显示 ViewControllerA
但仍处于横向模式。谁能帮助我了解如何以肖像模式显示它?
我尝试了以下方法,但对我没有任何作用:
在 viewWillAppear
中的 ViewControllerA
中添加了以下代码:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
在此视图控制器中还添加了以下功能:
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
请尝试
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
希望这可以帮助您使用 supportedInterfaceOrientationsForWindow
Appdelegate.h
@property () BOOL restrictRotation;
在Appdelegate.m
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
//restrictRotation is BOOL value
if(self.restrictRotation)//set the bool value to view controller which you want to load in landscape
return UIInterfaceOrientationMaskLandscape;
else
return UIInterfaceOrientationMaskAll;
}
//在你需要的地方调用这个
requiredViewController.m
-(void) restrictRotation:(BOOL) restriction
{
appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
//在ViewdidLoad()
- (void)viewDidLoad
{
[super viewDidLoad];
[self restrictRotation:YES];//Set YES if required in Landscape mode
}
使用下面的代码
-(void) viewDidAppear:(BOOL)animated
{
[UIViewController attemptRotationToDeviceOrientation];
[super viewDidAppear:animated];
}
&也在viewWIllAppear写
[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"]