How to rotate simulator using Swift code in xcode ui test
Asked Answered
E

2

17

I have a scenario where I have to rotate my simulator while xcode ui test is going.

To rotate the simulator, I use the below code

UIDevice.currentDevice().orientation
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")

but it is not working.

Is there any solution to rotate the simulator using Swift code in xcode ui test?

Elater answered 26/9, 2016 at 12:15 Comment(1)
TRY THIS How To Rotate SimulatorTimorous
Q
24

Have you tried this?

Swift 2.x

XCUIDevice.sharedDevice().orientation = .LandscapeLeft  
XCUIDevice.sharedDevice().orientation = .Portrait  

Swift 3.0

XCUIDevice.shared().orientation = .landscapeLeft
XCUIDevice.shared().orientation = .portrait

Swift 5.x

XCUIDevice.shared.orientation = .landscapeLeft
XCUIDevice.shared.orientation = .portrait
Quean answered 26/9, 2016 at 12:39 Comment(5)
Np glad to help @ElaterQuean
@Rashwan if i want to print the state of Simulator in console?.Timorous
Means if i rotate my simulator to portrait mode, it will print portrait as well as lndscapeTimorous
Try print(XCUIDevice.shared().orientation)@TimorousQuean
Is it just me or does this actually not rotate the simulator "physical device", only the interface orientation?Nurse
G
4

Swift 4:

XCUIDevice.shared.orientation = .landscapeLeft
XCUIDevice.shared.orientation = .portrait
Geometrician answered 26/2, 2018 at 13:13 Comment(1)
@BijenderSinghShekhawat it is the simulated device, you need to import XCTestWindbag

© 2022 - 2024 — McMap. All rights reserved.