1

I'm working on an app that is to be in portrait mode when it is launched. After that, I add an SDK for some purpose to my project. In the process of the integrating the SDK needs both Portrait and Landscape Right checked in the General pane. So I set it as given in the SDK integration manual.

enter image description here

and after that when I run my project in a device and rotate it to right the app device orientation is changing. How can I set the orientation is fixed without disturbing the SDK.

Ramakrishna
  • 712
  • 8
  • 26
  • can you clear the Question exactly what you want to do if you set the landscape right then orientation of-course it will change. – Bhupat Bheda Apr 24 '17 at 07:18
  • I want to my app will support only portrait mode. I know that if i give the landscape right it will change the orientation. If i remove the landscape right my sdk is not working and it crshing with some error like `Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES`. – Ramakrishna Apr 24 '17 at 07:24
  • Check out this answer: http://stackoverflow.com/a/43359827/7715250 – J. Doe Apr 24 '17 at 07:31

3 Answers3

3

if you're having trouble to set orientation right after the app launches then you might need to stop orientation for each view controller or one of it's base navigation controller

Swift 3:

class YourBaseNavigationController: UINavigationController {
override func viewDidLoad() {
    super.viewDidLoad()
    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
}
override var shouldAutorotate: Bool {
    return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return .portrait
}}

Use YourBaseNavigationController as your navigation controller.Basically all view controller within this navigation controller will be force to support portrait mode only.

  • I will try this solution. – Ramakrishna Apr 24 '17 at 07:26
  • what should I have to do if I set my root view controller to navigation controller programmatically? `nav = UINavigationController(rootViewController: SyncVc)` – Ramakrishna Apr 24 '17 at 09:20
  • So instead of using UINavigationController here, You should use YourBaseNavigationController. `YourBaseNavigationController(rootViewController: SyncVc)` –  Apr 24 '17 at 09:21
  • i created an extension for navigation controller in my app delegate and now it's working fine. – Ramakrishna Apr 24 '17 at 09:42
  • What Should I have to do if i have two UINavigation Controllers. Description: 1. I'm setting a view controller to the initial navigation controller root view. 2. After login, I'm having an SWRevealView controller. And there is another navigation controller as rear view. – Ramakrishna Apr 25 '17 at 06:15
2

unmark the checks Landscape left and landscape right

  • I know that if I uncheck the Landscape left and landscape right it will work in portrait mode. But if i uncheck those options i have problems with my SDK. – Ramakrishna Apr 24 '17 at 07:31
0

You need to disable orientation through code.

Add this code in your RootViewController:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);//set 'return NO'; for stop orientation
}

Swift :

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Portrait
}
KKRocks
  • 8,222
  • 1
  • 18
  • 84