简体   繁体   中英

When I rotate device and click tab item the views of the opened page remain in landscape mode

can someone help please .I support portrait mode on my device. But only one view controller can use landscape mode. When I open the view controller -which supports landscape mode-and click tabBar item, device returns to portrait mode but view is still on landscape mode.

These are my appDelegate codes:

   //Orientation Variables
    var myOrientation: UIInterfaceOrientationMask = .portrait

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

    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")
      }
    }

This is my landscape supported ViewController:

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

Also I have right navigation bar button. When user clicks on the button, screen will turn I have two functions for rotation

//MARK: Rotate Device to right
    func rotateToLandsScapeDevice(){
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.orientationLock = .landscapeRight;
        appDelegate.orientationLock = .allButUpsideDown;
        UIDevice.current.setValue(UIInterfaceOrientation.landscapeRight.rawValue, forKey: "orientation")
        UIView.setAnimationsEnabled(true)
        buttonClicked = false
    }


    //MARK: Rotate Device to portrait
    func rotateToPotraitScapeDevice(){
        
      let appDelegate = UIApplication.shared.delegate as! AppDelegate
        
        appDelegate.orientationLock = .portrait;
        UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
        UIView.setAnimationsEnabled(true)
        
        buttonClicked = true
    }

When view is dissapear I turn to portrait mode:

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

       AppDelegate.AppUtility.lockOrientation(.portrait)
        
        UIView.setAnimationsEnabled(true)
        
    
    }

The rotation works fine when I click back it rotates portrait mode. But when I click tabbar item view looks like this:

在此处输入图像描述 在此处输入图像描述

In my TabBarController class I detect when user clicking items. Then I change device rotation.

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
          
          appDelegate.orientationLock = .portrait;
          UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
          UIView.setAnimationsEnabled(true)
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM