应该自动旋转不适用于导航控制器swift 2 [英] shouldAutorotate not working with navigation controllar swift 2
问题描述
我的应用程序使用了几个库.基本上 KYDrawerController .我希望我的应用程序仅使用potraite Orientation,但对于一个ViewControllar,我既需要景观又需要Potrait.
my app uses several libraryies. Bassically KYDrawerController. I want my App to use only potraite Orientation but for one ViewControllar i need both landscape and Potrait.
我在互联网上搜索了几乎所有的解决方案.每个解决方案都是将NavigationControllar和Override ShouldAutoRotate方法子类化.但是没有一个为我工作.
I have search about allmost every solution on the internet. every solution was to subclass the NavigationControllar and Override ShouldAutoRotate method. but non of them worked for me.
灰色视图是KYDrawerControllar视图,它是一种库,用作Android之类的NavigationDrawer.
the gray color View is the KYDrawerControllar view which is an libry uses to work as NavigationDrawer like Android.
我为Navigation Controllar创建了一个自定义类,并将其子类化为所需的ViewControllar的Navigation Controllar.
I have created a custom Class for Navigation Controllar and subclass it to the requierd ViewControllar's 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
here is the 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.
and i am using StoryBoard segues to present ViewControllars.
请有人帮我解决这个问题.
please someone help me with this.
推荐答案
这是我的方式:
-
您的
-
:
appdelegae.swift
中的in your
appdelegae.swift
:
var shouldSupportAllOrientation = false
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if (shouldSupportAllOrientation == true){
return UIInterfaceOrientationMask.All
}
return UIInterfaceOrientationMask.Portrait
}
在您的进入视图中进入所有方向视图(更改为支持所有方向,这里我以一个按钮为例):
in your entry view which enter the all orientation view (change to support all orientation, here I use a button as an example):
@IBAction func next(sender: UIButton) {
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appdelegate.shouldSupportAllOrientation = true
self.performSegueWithIdentifier("next", sender: self)
}
在进入视图中进入所有方向视图(将方向更改为仅支持纵向):
in your entry view which enter the all orientation view (change the orientation to support only Portrait):
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除外);您应该在项目的常规信息中选中要求全屏显示",以确保所有方向视图都可以进入横向.
finally, you may find this works on all iPhone device and iPad ipad2 except iPad air iPad pro; you should check the "requires full screen" in your project general info to ensure you all orientation view can enter landscape.
这篇关于应该自动旋转不适用于导航控制器swift 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!