【问题标题】:shouldAutorotate not working with navigation controllar swift 2shouldAutorotate 不能与导航控制器 swift 2 一起使用
【发布时间】:2015-12-02 07:41:43
【问题描述】:

我的应用程序使用了几个库。基本上是KYDrawerController。我希望我的应用程序仅使用 Potrait 方向,但对于一个 ViewControllar,我需要横向和 Potrait。

我搜索了互联网上几乎所有的解决方案。每个解决方案都是继承 NavigationControllar 和 Override ShouldAutoRotate 方法。但没有一个对我有用。

here is the closer View of the entire StoryBoard

灰色视图是 KYDrawerControllar 视图,它是一个库,可以像 Android 一样用作 NavigationDrawer。

我为 Navigation Controllar 创建了一个自定义类,并将其子类化为所需的 ViewControllar 的 Navigation Controllar。

这是代码

class SettingsNavigation: UINavigationController {

override func viewDidLoad() {
    super.viewDidLoad()
   NSLog("visibleControllar", self.visibleViewController!.debugDescription)
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func shouldAutorotate() -> Bool {
    //return self.visibleViewController!.shouldAutorotate()
    return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Portrait
}

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return UIInterfaceOrientation.Portrait
}



/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}

这里是 ViewControllar

class Settings: UIViewController {

// MARK: - Outlets
@IBOutlet var profileImage: UIImageView!
@IBOutlet var profileName: UILabel!
@IBOutlet var profileRegistrationType: UILabel!
@IBOutlet var logOutButton: FUIButton!
@IBOutlet var subscriptionType: UILabel!
@IBOutlet var subscriptionRegistrationType: UILabel!
@IBOutlet var upgradeSubscriptionButton: FUIButton!



override func shouldAutorotate() -> Bool {
   /* if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
        UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||
        UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
            return false;
    }
    else {
        return true;
    }*/

    return false

}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Portrait
} }

我正在使用 StoryBoard segues 来展示 ViewControllars。

请有人帮我解决这个问题。

【问题讨论】:

    标签: uinavigationcontroller swift2 ios9 xcode7 screen-orientation


    【解决方案1】:

    这是我的方式:

    1. 在你的appdelegae.swift:

      var shouldSupportAllOrientation = false
      func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
          if (shouldSupportAllOrientation == true){
              return UIInterfaceOrientationMask.All
          }
          return UIInterfaceOrientationMask.Portrait
      }
      
    2. 在进入全向视图的入口视图中(改为支持全向视图,这里我以按钮为例):

      @IBAction func next(sender: UIButton) {
          let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
          appdelegate.shouldSupportAllOrientation = true
          self.performSegueWithIdentifier("next", sender: self)
      }
      
    3. 在进入所有方向视图的入口视图中(将方向更改为仅支持纵向):

      override func viewWillAppear(animated: Bool) {
          super.viewWillAppear(animated)
          let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
          appdelegate.shouldSupportAllOrientation = false
      }
      
    4. 最后,您可能会发现这适用于除 iPad air iPad pro 之外的所有 iPhone 设备和 iPad ipad2;您应该检查项目常规信息中的“需要全屏”,以确保所有方向视图都可以进入横向。

    【讨论】:

    • 谢谢你,我会试试这个并告诉你:)
    • @GishanthaJayasooriya 它有效。但对于 ipad air 和 pro,您需要检查是否需要全屏
    • @Xingou 谢谢。节省了我的时间
    猜你喜欢
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 2018-06-15
    • 1970-01-01
    相关资源
    最近更新 更多