Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force device rotate issue on iOS

Tags:

ios

swift

I am trying to make just one view of the landscape and other all views are on the portrait. It works but there is an issue on every second-time screen doesn't rotate at all.

Here is the scenario that I am talking about: enter image description here

On every second time, device rotation doesn't work.

Here is my code

AppDelegate.swift

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return .portrait
}

Controller1.swift

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}

override var shouldAutorotate: Bool {
    return true
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    AppUtility.lockOrientation(.portrait)
}

DetailController.swift

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    AppUtility.lockOrientation(.landscape, andRotateTo: .landscapeLeft)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    if isMovingFromParentViewController {
        AppUtility.lockOrientation(.portrait, andRotateTo: .portrait)
    }
}

Code that effects the rotation

struct AppUtility {

static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
    if let delegate = UIApplication.shared.delegate as? AppDelegate {
        delegate.orientationLock = orientation
    }
}

static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
    self.lockOrientation(orientation)
    UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
}

}

So, How to force rotate detail view every time it loads?

like image 474
EI Captain v2.0 Avatar asked Dec 15 '18 08:12

EI Captain v2.0


People also ask

Why wont my iOS screen Rotate?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off. Turn your iPhone sideways.

Why will my device not Rotate?

Turn On Screen Rotation From Android Settings In case you have a non-functional tile in Quick Settings, this method should fix your problem. Launch the Settings app on your Android phone. Tap Display in the list of items. Turn on the option that says Auto-rotate screen.

Is there a way to force apps to Rotate?

Android Settings Start by going to Settings => Display and locate the “Device rotation” setting. On my personal cell phone, tapping this will reveal two options: “Rotate the contents of the screen,” and “Stay in portrait view.”


1 Answers

Step 1:- Please define a below-mentioned variable in app delegate.

var shouldRotate : Bool = false

Step 2:- Implement the following delegate method as below.

//MARK:- Set Device Orientation Delegate Method

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return shouldRotate ? .landscapeRight : .portrait
}

Step 3 :- Define constant class and add "Set Portrait Orientation" and "Set Landscape Right Orientation" function in the class.

//MARK:- Set Portrait Orientation
func setPortraitOrientation() {
    appDelegateInstance.shouldRotate = false
    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
}

//MARK:- Set Landscape Right Orientation

func setLandscapeRightOrientation() {
   appDelegateInstance.shouldRotate = true
   UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
}

Step 4:- Use the following code in your class (Target landscape class)

Step 4.1:- In view will appear call set Landscape Right Orientation function shown below.

 override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    setLandscapeRightOrientation()
 }

Step 4.2 :- When you leave the screen implement the following code in the action.

//MARK:- Left Bar Button Tapped Delegate Method

func leftBarButtonTapped() {
    _ = self.navigationController?.popViewController(animated: false)
    setPortraitOrientation()
}

Happy Coding! Cheers!!

like image 102
Dipendra Avatar answered Sep 16 '22 13:09

Dipendra