我的应用使用了多个库。基本上 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。
请有人帮助我。
最佳答案
这是我的方法:
在你的
appdelegae.swift
中:var shouldSupportAllOrientation = false func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { if (shouldSupportAllOrientation == true){ return UIInterfaceOrientationMask.All } return UIInterfaceOrientationMask.Portrait }
在你进入全向 View 的入口 View 中(改为支持全向,这里我以一个按钮为例):
@IBAction func next(sender: UIButton) { let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate appdelegate.shouldSupportAllOrientation = true self.performSegueWithIdentifier("next", sender: self) }
在进入所有方向 View 的入口 View 中(更改方向以仅支持纵向):
override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate appdelegate.shouldSupportAllOrientation = false }
最后,您可能会发现这适用于除 iPad air iPad pro 之外的所有 iPhone 设备和 iPad ipad2;您应该在您的项目一般信息中勾选“需要全屏”,以确保您所有方向的 View 都可以进入横向。
关于uinavigationcontroller - shouldAutorotate 不适用于导航 Controller swift 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34037274/