35.让指定页面横屏
假设有两视图页面A和B,A竖屏显示,B要求横屏显示,C竖屏显示,NavigationController的root view是A,在storyboard中绑定好类名和ID,结构大概这样:
Paste_Image.png在AppDelegate中编写代码让APP能支持所有屏目方向(或者在General->Deployment Info->Device Orientation中勾选所有方向也可以):
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
return .All
}
同时设置,不然在ipad上,页面方向会随屏目转动
Paste_Image.png
在NavigationController中重写(假设window.rootViewController是NavigationController的情况;如果顶层是TabBarController则在TabBarController重写下面的代码;如果是ViewController则在ViewController中重写下面的代码),让页面默认是竖直方向:
import UIKit
class NavigationController: UINavigationController {
//这里需要设置为false,否则在横屏下将出现竖屏的键盘
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Portrait
}
}
override var preferredInterfaceOrientationForPresentation : UIInterfaceOrientation {
return .portrait
}
接下来在B中编写它支持的方向:
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Landscape
}
//此方法设置viewController的初始显示方向. 此方法也仅有在当前viewController是rootViewController或者是modal模式时才生效.
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return .LandscapeLeft
}
下面在A的按扭中增加代码显示视图B的代码,当然显示的方式是modal的方式,可以简单测试一下:
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let b = storyboard.instantiateViewControllerWithIdentifier("BViewController")
self.presentViewController(b, animated: false, completion: nil)
到上面这一步横屏显示是没问题了,但是会发现C的显示也会是modal模式,如果希望C显示时具有NavigationBar,不影响其它页面对B的调用,应当如何实现?这是接下来要讲的一个技巧,使得横屏时能被正常push进NavigationController的vc列表中,而且c也显示在navigationController的vc列表中,有NavigationBar.
我们只需要给B视图增加一个辅助页面D来进行对B的管理.这样A->D->C,这样调用显示肯定是最正常的NavigationController VC列表.我们只需要在显示D的同时显示B,D消失时B消失,就能达我们的要求了.
D的实现:
import UIKit
class DViewController: UIViewController {
var b:BViewController!
override func viewDidLoad() {
self.view.backgroundColor = UIColor.whiteColor()
if b == nil {
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
self.b = storyboard.instantiateViewControllerWithIdentifier("BViewController") as! BViewController
}
}
override func viewWillAppear(animated: Bool) {
if b.view.window == nil {
self.navigationController?.presentViewController(b, animated: false, completion: nil)
}
}
}
B的实现:
import UIKit
class BViewcontroller: UIViewController {
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Landscape
}
//刚进入页面时的方向
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return .LandscapeLeft
}
@IBAction func onBack(sender: AnyObject) {
let nv = self.presentingViewController as? UINavigationController
nv?.popViewControllerAnimated(false)
self.dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func onTouchUp(sender: UIButton) {
let nv = self.presentingViewController
let sb = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
let c = sb.instantiateViewControllerWithIdentifier("CViewController")
self.dismissViewControllerAnimated(false, completion: nil)
nv?.showViewController(c, sender: nil)
}
}
简单的理解为B和D绑定在一起,同时显示,同时隐藏
源码:https://github.com/ghyh22/PageRotateExample
参考资料:
http://www.cnblogs.com/jhzhu/p/3480885.html