如何更改在XCTest for iOS上测试的方向?

Mey*_*pan 4 objective-c ios xctest xctestcase

我正在尝试使用XCTestCase在不同方向上测试我的iOS应用。我需要一种以编程方式更改方向的方法。我尝试使用2种方法进行此操作,但两种方法均未更改方向(方向仍为UIInterfaceOrientationPortrait)。

尝试1

[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;`
Run Code Online (Sandbox Code Playgroud)

尝试2

[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];`
Run Code Online (Sandbox Code Playgroud)

当我阅读[[UIApplication sharedApplication] statusBarOrientation]时,是否还有另一种方法可以更改方向以进行测试?

小智 6

使用下面的解决方案。在设置方法中执行此操作。

[[UIDevice currentDevice] setValue:
                      [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                            forKey:@"orientation"];
Run Code Online (Sandbox Code Playgroud)

它对我有用。


dim*_*4eg 5

SWIFT代码:

XCUIDevice.shared.orientation = UIDeviceOrientation.portrait;
Run Code Online (Sandbox Code Playgroud)

  • 除非您的测试目标是 XCUITest 目标,否则这不再适用于 Xcode 13。 (2认同)