2016-02-25 51 views
2

我想在不同的方向使用XCTestCase测试我的iOS应用程序。我需要一种编程方式来改变方向。我尝试了2种方法,但都没有改变方向(方向保持为UIInterfaceOrientationPortrait)。如何更改XCTest for iOS上的测试方向?

尝试1

[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;` 

尝试2

[[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];` 

当我读到[UIApplication的sharedApplication] statusBarOrientation] 有另一种方式来改变用于测试目的的方向是什么?

回答

3

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

[[UIDevice currentDevice] setValue: 
         [NSNumber numberWithInteger: UIInterfaceOrientationPortrait] 
          forKey:@"orientation"]; 

它为我工作。

1

这应该工作,我随意改变方向,因为我跑多次试验,我希望这是有用的

 // Set a random device orientation 
    let orient = [UIDeviceOrientation.portrait , UIDeviceOrientation.portraitUpsideDown, UIDeviceOrientation.landscapeLeft, UIDeviceOrientation.landscapeRight] 
    XCUIDevice.shared().orientation = orient[Int(arc4random_uniform(UInt32(orient.count)))]