好吧,由于没有人回答我之前的问题,我开始相信可能没有简单的方法可以做到这一点。但我很乐观。这是我的问题:
在我的应用程序中,我使用常规 UIButton 从 ViewControllerOne 切换到 ViewControllerTwo。 ViewControllerOne 始终处于横向模式。 ViewControllerTwo 应该始终处于纵向模式。但是,当我在横向 ViewControllerOne 中按下按钮时,ViewControllerTwo 也处于横向模式,尽管我希望它切换到纵向模式,无论按下按钮时用户如何旋转设备。
我将以下代码添加到我的 AppDelegate 中:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
NSUInteger orientations = UIInterfaceOrientationMaskAll;
if (self.window.rootViewController) {
UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presented supportedInterfaceOrientations];
}
return orientations;
}
我将其添加到我的 ViewController 中,该控制器应该仅处于纵向模式:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
-(void)viewWillAppear:(BOOL)animated
{
UIApplication* application = [UIApplication sharedApplication];
application.statusBarOrientation = UIInterfaceOrientationPortrait;
}
有没有办法告诉 viewController 进入纵向模式,即使之前的视图是横向的?也许我可以创建一个自定义segue,当按下按钮时强制视图处于纵向模式?这里最好的/官方的解决方案是什么?
根据我对问题的理解,我假设您希望第二个视图控制器为纵向,第一个视图控制器为横向。
对于第二个视图控制器,添加此方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
对于第一个视图控制器:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscape);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)