Ios 使用segues调整当前屏幕方向
故事板有两个viewcontroller-s,一个用于纵向(Ios 使用segues调整当前屏幕方向,ios,segue,uiinterfaceorientation,Ios,Segue,Uiinterfaceorientation,故事板有两个viewcontroller-s,一个用于纵向(vport),另一个用于横向(VCLand)。我希望应用程序在设备旋转时自动切换到正确的布局。 为此,我有两个从VSPort到VCLand的区段portToLand,以及反向的LandToPort。然后我覆盖将旋转屏幕方向,如下所示 对于VCPort类: override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientatio
vport
),另一个用于横向(VCLand
)。我希望应用程序在设备旋转时自动切换到正确的布局。
为此,我有两个从VSPort到VCLand的区段portToLand
,以及反向的LandToPort
。然后我覆盖将旋转屏幕方向
,如下所示
对于VCPort类:
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval)
{
if toInterfaceOrientation.isLandscape {
switchViewController()
}
}
func switchViewController() {
performSegueWithIdentifier("portToLand", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
if UIApplication.sharedApplication().statusBarOrientation.isLandscape {
switchViewController()
}
}
对于VCLand类,我有isPortrait
而不是isLandscape
和“landToPort”
而不是“portToLand”
如果故事板从纵向开始,一切正常。当故事板启动时,设备处于横向(例如,使用IPad Retina),vPort仍然作为链中的第一个ViewController获得控制权,并且不会出现旋转屏幕。在这种情况下,要使应用程序切换到横向布局,我将以下代码放入VCPort类的viewDidLoad
:
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval)
{
if toInterfaceOrientation.isLandscape {
switchViewController()
}
}
func switchViewController() {
performSegueWithIdentifier("portToLand", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
if UIApplication.sharedApplication().statusBarOrientation.isLandscape {
switchViewController()
}
}
使用调试器跟踪显示调用了performsguewithidentifier,但是VCLand没有得到控制,应用程序仍然显示VCPort布局!假设我做得太早,我尝试了viewwillbeen
而不是viewDidLoad
——没有区别
谢谢您的评论。在切换视图之前请稍等片刻。并将代码移动到ViewWillDisplay,因为在应用程序暂停时方向可能会改变:
var timer : NSTimer?
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let deviceOrientation = UIDevice.currentDevice().orientation
var isLandscape : Bool;
if deviceOrientation == UIDeviceOrientation.Unknown {
isLandscape = UIApplication.sharedApplication().statusBarOrientation.isLandscape
} else {
isLandscape = deviceOrientation.isLandscape
}
if isLandscape {
self.view.hidden = true
timer = NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: Selector("switchViewController"), userInfo: nil, repeats: false)
} else {
self.view.hidden = false
}
}
override func viewWillDisappear(animated: Bool) {
if (timer != nil) {
timer!.invalidate()
timer = nil
}
}
这不是一个好主意。每次旋转时,您都在创建一个新的视图控制器,因此您将添加越来越多的控制器,直到内存耗尽。纵向和横向的设计完全不同?如果没有,则可以使用Autolayout constraints.rdelmar。当方向返回时,我很乐意重用相同的控制器。可能吗?顺便说一句,据我记忆所及,苹果的一篇文章提到segues是一种处理多方位的正确方法。控件是相同的,但彼此相对的位置完全不同,因此Autolayout没有帮助