gpt4 book ai didi

iphone总是返回UIInterfaceOrientationPortrait

转载 作者:行者123 更新时间:2023-12-03 20:54:39 31 4
gpt4 key购买 nike

我需要确保当我的 UIViewController 加载时,它会根据需要旋转。

我已经实现了 shouldAutorotateToInterfaceOrientation 方法,并且一切正常,除非应用程序首次加载时(当 iphone 处于横向模式时)或从处于纵向模式的 UIViewController 开始时 interfaceOrientation[[UIDevice currentDevice]orientation] 始终返回纵向模式!

我一直在阅读博客甚至一些SO问题,但它们似乎都没有解决根本问题。

我正在使用标准 UINavigationController 应用程序。所以第一个 View 是第一个插入堆栈的 Controller 。我也尝试询问 self.navigationController 的方向,但也没有成功。

非常感谢这里的任何帮助。

编辑:这是我的 viewWillAppear 代码

- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[self navigationController] setNavigationBarHidden:YES animated:NO];

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
[self setPortraitMode];
else
[self setLandscapeMode];
}

编辑2

这是我打印当前内容的方法

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
NSLog(@"currentDevice: %d", (int)orientation);
NSLog(@"statusbar: %d", (int)[UIApplication sharedApplication].statusBarOrientation);
NSLog(@"nav controller: %d", (int)self.navigationController.interfaceOrientation);
NSLog(@"interface orientation: %d", (int)self.interfaceOrientation);

它们都打印出“1”

但是当我将此代码放入 willRotateToInterfaceOrientation 方法中时,它们会打印出正确的 int 值(1、2、3 或 4)。

最佳答案

不要使用 [[UIDevice currentDevice]orientation] 来检查方向,而是使用状态栏方向来获取确切的方向:- [UIApplication sharedApplication].statusBarOrientation 。我在检查 currentDevice 方向时也遇到问题,它并不总是返回准确的方向

关于iphone总是返回UIInterfaceOrientationPortrait,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5864949/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com