uinavigationcontroller - shouldAutorotate 不适用于导航 Controller swift 2

标签 uinavigationcontroller swift2 ios9 xcode7 screen-orientation

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

我在互联网上搜索了几乎所有的解决方案。每个解决方案都是子类化 NavigationControllar 并重写 ShouldAutoRotate 方法。但没有一个对我有用。

here is the closer View of the entire StoryBoard

灰色 View 是 KYDrawerControllar View ,它是一个像 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.
}
*/

这是 View Controller

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。

请有人帮助我。

最佳答案

这是我的方法:

  1. 在你的appdelegae.swift中:

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

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

    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;您应该在您的项目一般信息中勾选“需要全屏”,以确保您所有方向的 View 都可以进入横向。

关于uinavigationcontroller - shouldAutorotate 不适用于导航 Controller swift 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34037274/

相关文章:

uinavigationcontroller - 将 BarButtonItem 添加到模态导航 View Controller

一次推送显示两次 iOS 推送通知横幅

ios - SpriteKit 着色器崩溃 iOS 9 : SKDefaultShading, gl_FragCoord

ios - 根据内容的 UITextView 高度在 iOS 9 中是错误的

iphone - 关于 View Controller 中的导航 Controller

ios - 如何从导航栏中完全删除后退按钮?

ios - 在插入第二个 VC 时出现错误堆栈未更新

ios - 如何在 Xcode 中从 UIActivityViewController 共享用户位置

ios - 如何在应用程序终止或终止时点击推送通知后打开特定 Controller

ios - 无法滚动到 UITableView 中索引路径处的行