Portrait by default, How can some special screens auto rotated or force some screens to landscape?











up vote
2
down vote

favorite
1












In my app, the orientation is portrait by default, but in some special screens I want the viewcontroller to be auto rotated or to force the orientation to landscape.



in AppDelegate.swift :



class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

// portrait by default
var interfaceOrientations:UIInterfaceOrientationMask = .portrait {
didSet{
// force to portrait
if interfaceOrientations == .portrait {
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue,
forKey: "orientation")
}
// force to landscape
else if !interfaceOrientations.contains(.portrait){
UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue,
forKey: "orientation")
}
}
}

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


in only portrait vc:



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

appDelegate.interfaceOrientations = .portrait
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

appDelegate.interfaceOrientations = .portrait
}


in auto rotated vc:



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

appDelegate.interfaceOrientations = .allButUpsideDown
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

appDelegate.interfaceOrientations = .portrait
}


in only landscape vc:



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

appDelegate.interfaceOrientations = [.landscapeLeft, .landscapeRight]
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

appDelegate.interfaceOrientations = .portrait
}


but it still has bugs...Help me










share|improve this question




























    up vote
    2
    down vote

    favorite
    1












    In my app, the orientation is portrait by default, but in some special screens I want the viewcontroller to be auto rotated or to force the orientation to landscape.



    in AppDelegate.swift :



    class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    // portrait by default
    var interfaceOrientations:UIInterfaceOrientationMask = .portrait {
    didSet{
    // force to portrait
    if interfaceOrientations == .portrait {
    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue,
    forKey: "orientation")
    }
    // force to landscape
    else if !interfaceOrientations.contains(.portrait){
    UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue,
    forKey: "orientation")
    }
    }
    }

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


    in only portrait vc:



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

    appDelegate.interfaceOrientations = .portrait
    }

    override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    appDelegate.interfaceOrientations = .portrait
    }


    in auto rotated vc:



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

    appDelegate.interfaceOrientations = .allButUpsideDown
    }

    override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    appDelegate.interfaceOrientations = .portrait
    }


    in only landscape vc:



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

    appDelegate.interfaceOrientations = [.landscapeLeft, .landscapeRight]
    }

    override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    appDelegate.interfaceOrientations = .portrait
    }


    but it still has bugs...Help me










    share|improve this question


























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      In my app, the orientation is portrait by default, but in some special screens I want the viewcontroller to be auto rotated or to force the orientation to landscape.



      in AppDelegate.swift :



      class AppDelegate: UIResponder, UIApplicationDelegate {
      var window: UIWindow?

      // portrait by default
      var interfaceOrientations:UIInterfaceOrientationMask = .portrait {
      didSet{
      // force to portrait
      if interfaceOrientations == .portrait {
      UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue,
      forKey: "orientation")
      }
      // force to landscape
      else if !interfaceOrientations.contains(.portrait){
      UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue,
      forKey: "orientation")
      }
      }
      }

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


      in only portrait vc:



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

      appDelegate.interfaceOrientations = .portrait
      }

      override func viewWillDisappear(_ animated: Bool) {
      super.viewWillDisappear(animated)

      appDelegate.interfaceOrientations = .portrait
      }


      in auto rotated vc:



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

      appDelegate.interfaceOrientations = .allButUpsideDown
      }

      override func viewWillDisappear(_ animated: Bool) {
      super.viewWillDisappear(animated)

      appDelegate.interfaceOrientations = .portrait
      }


      in only landscape vc:



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

      appDelegate.interfaceOrientations = [.landscapeLeft, .landscapeRight]
      }

      override func viewWillDisappear(_ animated: Bool) {
      super.viewWillDisappear(animated)

      appDelegate.interfaceOrientations = .portrait
      }


      but it still has bugs...Help me










      share|improve this question















      In my app, the orientation is portrait by default, but in some special screens I want the viewcontroller to be auto rotated or to force the orientation to landscape.



      in AppDelegate.swift :



      class AppDelegate: UIResponder, UIApplicationDelegate {
      var window: UIWindow?

      // portrait by default
      var interfaceOrientations:UIInterfaceOrientationMask = .portrait {
      didSet{
      // force to portrait
      if interfaceOrientations == .portrait {
      UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue,
      forKey: "orientation")
      }
      // force to landscape
      else if !interfaceOrientations.contains(.portrait){
      UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue,
      forKey: "orientation")
      }
      }
      }

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


      in only portrait vc:



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

      appDelegate.interfaceOrientations = .portrait
      }

      override func viewWillDisappear(_ animated: Bool) {
      super.viewWillDisappear(animated)

      appDelegate.interfaceOrientations = .portrait
      }


      in auto rotated vc:



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

      appDelegate.interfaceOrientations = .allButUpsideDown
      }

      override func viewWillDisappear(_ animated: Bool) {
      super.viewWillDisappear(animated)

      appDelegate.interfaceOrientations = .portrait
      }


      in only landscape vc:



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

      appDelegate.interfaceOrientations = [.landscapeLeft, .landscapeRight]
      }

      override func viewWillDisappear(_ animated: Bool) {
      super.viewWillDisappear(animated)

      appDelegate.interfaceOrientations = .portrait
      }


      but it still has bugs...Help me







      ios swift rotation orientation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 13:53









      Ratul Sharker

      2,14211524




      2,14211524










      asked Nov 8 at 10:20









      desmond_yy

      111




      111
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote













          You should force the rotation with:



          UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")


          You also have to enable autoRotation



          override var shouldAutorotate: Bool{
          return true
          }


          And of course, in your project settings you have to enable both portrait and landscapes modes.






          share|improve this answer




























            up vote
            0
            down vote













            You can custom your Portrait View Controller and Landscape View Controller. LandscapeViewController like this:



            ```



             override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
            }

            override var shouldAutorotate: Bool {
            return true
            }

            override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return .landscapeRight
            }

            override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
            return .landscapeRight
            }


            ```



            Some points need to notice: UIViewController can't decide to rotate automatically, you can use UINavigationController to fetch the value of rotation property:



            override var shouldAutorotate: Bool {
            return (self.topViewController?.shouldAutorotate)!
            }

            override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
            return (self.topViewController?.supportedInterfaceOrientations)!
            }

            override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
            return (self.topViewController?.preferredInterfaceOrientationForPresentation)!
            }





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














               

              draft saved


              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205704%2fportrait-by-default-how-can-some-special-screens-auto-rotated-or-force-some-scr%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote













              You should force the rotation with:



              UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")


              You also have to enable autoRotation



              override var shouldAutorotate: Bool{
              return true
              }


              And of course, in your project settings you have to enable both portrait and landscapes modes.






              share|improve this answer

























                up vote
                0
                down vote













                You should force the rotation with:



                UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")


                You also have to enable autoRotation



                override var shouldAutorotate: Bool{
                return true
                }


                And of course, in your project settings you have to enable both portrait and landscapes modes.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You should force the rotation with:



                  UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")


                  You also have to enable autoRotation



                  override var shouldAutorotate: Bool{
                  return true
                  }


                  And of course, in your project settings you have to enable both portrait and landscapes modes.






                  share|improve this answer












                  You should force the rotation with:



                  UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")


                  You also have to enable autoRotation



                  override var shouldAutorotate: Bool{
                  return true
                  }


                  And of course, in your project settings you have to enable both portrait and landscapes modes.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 10:51









                  Juan Curti

                  1,8501130




                  1,8501130
























                      up vote
                      0
                      down vote













                      You can custom your Portrait View Controller and Landscape View Controller. LandscapeViewController like this:



                      ```



                       override func viewDidAppear(_ animated: Bool) {
                      super.viewDidAppear(animated)
                      UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
                      }

                      override var shouldAutorotate: Bool {
                      return true
                      }

                      override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
                      return .landscapeRight
                      }

                      override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
                      return .landscapeRight
                      }


                      ```



                      Some points need to notice: UIViewController can't decide to rotate automatically, you can use UINavigationController to fetch the value of rotation property:



                      override var shouldAutorotate: Bool {
                      return (self.topViewController?.shouldAutorotate)!
                      }

                      override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
                      return (self.topViewController?.supportedInterfaceOrientations)!
                      }

                      override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
                      return (self.topViewController?.preferredInterfaceOrientationForPresentation)!
                      }





                      share|improve this answer



























                        up vote
                        0
                        down vote













                        You can custom your Portrait View Controller and Landscape View Controller. LandscapeViewController like this:



                        ```



                         override func viewDidAppear(_ animated: Bool) {
                        super.viewDidAppear(animated)
                        UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
                        }

                        override var shouldAutorotate: Bool {
                        return true
                        }

                        override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
                        return .landscapeRight
                        }

                        override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
                        return .landscapeRight
                        }


                        ```



                        Some points need to notice: UIViewController can't decide to rotate automatically, you can use UINavigationController to fetch the value of rotation property:



                        override var shouldAutorotate: Bool {
                        return (self.topViewController?.shouldAutorotate)!
                        }

                        override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
                        return (self.topViewController?.supportedInterfaceOrientations)!
                        }

                        override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
                        return (self.topViewController?.preferredInterfaceOrientationForPresentation)!
                        }





                        share|improve this answer

























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You can custom your Portrait View Controller and Landscape View Controller. LandscapeViewController like this:



                          ```



                           override func viewDidAppear(_ animated: Bool) {
                          super.viewDidAppear(animated)
                          UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
                          }

                          override var shouldAutorotate: Bool {
                          return true
                          }

                          override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
                          return .landscapeRight
                          }

                          override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
                          return .landscapeRight
                          }


                          ```



                          Some points need to notice: UIViewController can't decide to rotate automatically, you can use UINavigationController to fetch the value of rotation property:



                          override var shouldAutorotate: Bool {
                          return (self.topViewController?.shouldAutorotate)!
                          }

                          override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
                          return (self.topViewController?.supportedInterfaceOrientations)!
                          }

                          override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
                          return (self.topViewController?.preferredInterfaceOrientationForPresentation)!
                          }





                          share|improve this answer














                          You can custom your Portrait View Controller and Landscape View Controller. LandscapeViewController like this:



                          ```



                           override func viewDidAppear(_ animated: Bool) {
                          super.viewDidAppear(animated)
                          UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
                          }

                          override var shouldAutorotate: Bool {
                          return true
                          }

                          override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
                          return .landscapeRight
                          }

                          override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
                          return .landscapeRight
                          }


                          ```



                          Some points need to notice: UIViewController can't decide to rotate automatically, you can use UINavigationController to fetch the value of rotation property:



                          override var shouldAutorotate: Bool {
                          return (self.topViewController?.shouldAutorotate)!
                          }

                          override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
                          return (self.topViewController?.supportedInterfaceOrientations)!
                          }

                          override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
                          return (self.topViewController?.preferredInterfaceOrientationForPresentation)!
                          }






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 9 at 2:11

























                          answered Nov 9 at 1:58









                          ZeroOnet

                          65




                          65






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205704%2fportrait-by-default-how-can-some-special-screens-auto-rotated-or-force-some-scr%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Schultheiß

                              Liste der Kulturdenkmale in Wilsdruff

                              Android Play Services Check