当前位置:  开发笔记 > 编程语言 > 正文

shouldAutorotate不使用导航控制器swift 2

如何解决《shouldAutorotate不使用导航控制器swift2》经验,为你挑选了1个好方法。

我的应用程序使用了几个库.Bassically KYDrawerController.我希望我的应用程序仅使用potraite Orientation,但对于一个ViewControllar我需要landscape和Potrait.

我搜索了互联网上的所有解决方案.每个解决方案都是子类化NavigationControllar和Override ShouldAutoRotate方法.但他们没有为我工作.

而这里是整个故事板的近距离观察

灰色的View是KYDrawerControllar视图,它是一个libry用来像Android这样的NavigationDrawer.

我已经为导航控制器创建了一个自定义类,并将其子类化为需要的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.

请有人帮我这个.



1> Xingou..:

这是我的方式:

    在你的appdelegae.swift:

    var shouldSupportAllOrientation = false
    func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
        if (shouldSupportAllOrientation == true){
            return UIInterfaceOrientationMask.All
        }
        return UIInterfaceOrientationMask.Portrait
    }
    

    在您的输入视图中进入所有方向视图(更改为支持所有方向,这里我使用一个按钮作为示例):

    @IBAction func next(sender: UIButton) {
        let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appdelegate.shouldSupportAllOrientation = true
        self.performSegueWithIdentifier("next", sender: self)
    }
    

    在您的输入视图中进入所有方向视图(更改方向以仅支持纵向):

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appdelegate.shouldSupportAllOrientation = false
    }
    

    最后,您可能会发现这适用于所有iPhone设备和iPad ipad2,除了iPad air iPad pro; 您应该检查项目常规信息中的"需要全屏",以确保您可以进入所有方向视图.

推荐阅读
k78283381
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有