Luc*_*uca 5 iphone rotation orientation ios swift
我正在编写一个带有一些固定(纵向)视图和一些依赖于方向的视图(纵向、左侧横向和右侧横向)的 iOS 应用程序。
视图包含在基于以下代码的自定义导航控制器中:
class CustomNavigationViewController: UINavigationController {
override func supportedInterfaceOrientations() -> Int {
return self.topViewController.supportedInterfaceOrientations()
}
override func shouldAutorotate() -> Bool {
return self.topViewController.shouldAutorotate()
}
}
Run Code Online (Sandbox Code Playgroud)
我正在实现supportedInterfaceOrientations()和shouldAutorotate()内部导航子控制器。
这是一个测试控制器
class TestViewController: UIViewController {
var shouldRotate: Bool = false
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
self.shouldRotate = true
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
convenience init(rotation shouldRotate: Bool) {
self.init(nibName: "TestViewController", bundle: nil)
self.shouldRotate = shouldRotate
}
override func supportedInterfaceOrientations() -> Int {
if shouldRotate == false {
return UIInterfaceOrientation.Portrait.rawValue
} else {
return super.supportedInterfaceOrientations()
}
}
override func shouldAutorotate() -> Bool {
return shouldRotate
}
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
println("from: \(fromInterfaceOrientation.rawValue)")
}
@IBAction func buttonTapped(sender: AnyObject) {
let newvc = TestViewController(rotation: true)
self.navigationController?.pushViewController(newvc, animated: true)
}
}
Run Code Online (Sandbox Code Playgroud)
第一个控制器在AppDelegatewith 中实例化rotation: false。其他控制器是rotation: true在点击按钮后创建和推动的。
这是视图的屏幕截图:
如果我在第一个(可旋转的)之后改变控制器的方向,我会得到以下结果:
控制器 xib 使用自动布局,如果我在点击按钮之前旋转设备,它会按预期工作,视图填充整个屏幕。
此外,如果我在手机处于横向状态时点击后退按钮,第一个视图将锁定在此状态:
我正在将应用程序部署到 iOS 8。
如何仅使第一个视图成为肖像,而其他视图在旋转后正确布局?
这可能并不重要,因为它只是打印出调试消息,但不要忘记它didRotateFromInterfaceOrientation(_:)在 iOS 8 中已被贬值,因为viewWillTransitionToSize(_:withTransitionCoordinator:)它取代了视图大小更改(包括旋转)的职责。
我认为您走在正确的轨道上,但不是覆盖两者shouldAutorotate()并且supportedInterfaceOrientations()仅覆盖supportedInterfaceOrientations().
我只是做了一个快速的 POC,看看它是否有效。在创建视图控制器(并使用 Swift 2)时我没有设置标志,但你明白了:
class RotatingViewController: UIViewController {
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.All
}
}
class FixedViewController: UIViewController {
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
}
class NavigationController: UINavigationController {
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return self.topViewController?.supportedInterfaceOrientations() ?? UIInterfaceOrientationMask.All
}
}
Run Code Online (Sandbox Code Playgroud)
我认为您的演示中发生的情况是,当您转换到固定的视图控制器时,您要求它进入纵向(确实如此),但不允许屏幕旋转回纵向模式。
所以..只需尝试删除您的“shouldAutorotate()”,CustomNavigationViewController看看是否有效。
| 归档时间: |
|
| 查看次数: |
2413 次 |
| 最近记录: |