当我旋转设备时,我想加载一个与Storyboard完全不同的MasterView。
在AppDelegate中我有
func rotated() {
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation)) {
// Here I would like to load LandscapeViewController
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)) {
// or load PortraitViewController
}
}
任何建议如何实现这一目标?
我知道有.loadView()
,但我不认为这是正确的方法,或者我做错了什么......
答案 0 :(得分:1)
您需要使用名为 viewWillTransitionToSize 的方法,因为在iOS 8中不推荐使用 didRotateFromInterfaceOrientation 。
最终代码看起来像这样:
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
if size.width > size.height {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var secondViewController = storyboard.instantiateViewControllerWithIdentifier("landscape") as? UIViewController
if let newVC = secondViewController{
//If you have a container add newVC as a childViewController, else present the view
println("landscape")
}
}
else if size.width < size.height{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var secondViewController = storyboard.instantiateViewControllerWithIdentifier("portrait") as? UIViewController
if let newVC = secondViewController{
//If you have a container add newVC as a childViewController, else present the view
println("portrait")
}
}
}
答案 1 :(得分:1)
您可以从故事板中实例化视图控制器。
如果你在一个UINavigationController中嵌入了PortraitViewController,那么你可以将LandscapeViewController推送到旋转状态吗?
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var viewController: LandscapeViewControllerViewController = storyboard.instantiateViewControllerWithIdentifier("LandscapeViewControllerViewController") as LandscapeViewController
var rootViewController = self.window!.rootViewController as UINavigationController
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation)) {
rootViewController.pushViewController(viewController, animated: true)
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation)) {
rootViewController.popViewController()
}