Yan*_*eph 69
嗨,适合LandscapeLeft和LandscapeRight (更新Swift 2.0)
你有这个信息
和UIController
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.LandscapeLeft,UIInterfaceOrientationMask.LandscapeRight]
}
Run Code Online (Sandbox Code Playgroud)
对于PortraitUpsideDown和Portrait使用它
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
}
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.Portrait ,UIInterfaceOrientationMask.PortraitUpsideDown]
}
Run Code Online (Sandbox Code Playgroud)
来自法国的消息,圣诞快乐!
编辑:
其他方案:
extension UINavigationController {
public override func shouldAutorotate() -> Bool {
if visibleViewController is MyViewController {
return true // rotation
} else {
return false // no rotation
}
}
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return (visibleViewController?.supportedInterfaceOrientations())!
}
}
Run Code Online (Sandbox Code Playgroud)
DrO*_*ild 50
您可以将这些方法粘贴到需要为肖像的每个视图的ViewController中:
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
Run Code Online (Sandbox Code Playgroud)
Dra*_*gos 40
斯威夫特3
如果视图控制器被嵌入的UINavigationController或的UITabBarController导航或标签栏控制器优先,并就自转和支撑取向决定取向旋转更为复杂.
在UINavigationController和UITabBarController上使用以下扩展,以便嵌入其中一个控制器的视图控制器可以做出决定:
UINavigationController扩展
extension UINavigationController {
override open var shouldAutorotate: Bool {
get {
if let visibleVC = visibleViewController {
return visibleVC.shouldAutorotate
}
return super.shouldAutorotate
}
}
override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
get {
if let visibleVC = visibleViewController {
return visibleVC.preferredInterfaceOrientationForPresentation
}
return super.preferredInterfaceOrientationForPresentation
}
}
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get {
if let visibleVC = visibleViewController {
return visibleVC.supportedInterfaceOrientations
}
return super.supportedInterfaceOrientations
}
}}
Run Code Online (Sandbox Code Playgroud)
UITabBarController扩展
extension UITabBarController {
override open var shouldAutorotate: Bool {
get {
if let selectedVC = selectedViewController{
return selectedVC.shouldAutorotate
}
return super.shouldAutorotate
}
}
override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
get {
if let selectedVC = selectedViewController{
return selectedVC.preferredInterfaceOrientationForPresentation
}
return super.preferredInterfaceOrientationForPresentation
}
}
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get {
if let selectedVC = selectedViewController{
return selectedVC.supportedInterfaceOrientations
}
return super.supportedInterfaceOrientations
}
}}
Run Code Online (Sandbox Code Playgroud)
现在,您可以覆盖要锁定的视图控制器中的supportedInterfaceOrientations,shouldAutoRotate和preferredInterfaceOrientationForPresentation,否则您可以忽略其他视图控制器中要继承应用程序plist中指定的默认方向行为的覆盖.
锁定特定方向
class YourViewController: UIViewController {
open override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
get {
return .portrait
}
}}
Run Code Online (Sandbox Code Playgroud)
禁用旋转
class YourViewController: UIViewController {
open override var shouldAutorotate: Bool {
get {
return false
}
}}
Run Code Online (Sandbox Code Playgroud)
更改首选接口方向以进行演示
class YourViewController: UIViewController {
open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{
get {
return .portrait
}
}}
Run Code Online (Sandbox Code Playgroud)
Viv*_*har 18
如果您的视图控制器属于导航控制器,则上述代码可能无法正常工作.如果是,则即使它本身具有不同的方向规则,它也必须遵守导航控制器的规则.更好的方法是让视图控制器自行决定,导航控制器将使用最顶层视图控制器的决定.
我们可以支持锁定到当前方向和自动旋转以使用UINavigationController上的这个通用扩展锁定特定方向: - :
extension UINavigationController {
public override func shouldAutorotate() -> Bool {
return visibleViewController.shouldAutorotate()
}
public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return (visibleViewController?.supportedInterfaceOrientations())!
}
}
Run Code Online (Sandbox Code Playgroud)
现在在视图控制器中我们可以
class ViewController: UIViewController {
// MARK: Autoroate configuration
override func shouldAutorotate() -> Bool {
if (UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
return true
}
else {
return false
}
}
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue) | Int(UIInterfaceOrientationMask.PortraitUpsideDown.rawValue)
}
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.谢谢
tes*_*sla 11
这将禁用视图的自动旋转:
override func shouldAutorotate() -> Bool {
return false;
}
Run Code Online (Sandbox Code Playgroud)
更新
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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果应用程序处于横向模式并且您显示必须以纵向模式显示的视图,这将允许应用程序将其方向更改为纵向(当然,当设备将旋转到此类方向时).
lob*_*art 11
如果有人想要答案,我想我得到了答案.试试这个:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait.toRaw().hashValue | UIInterfaceOrientationMask.PortraitUpsideDown.toRaw().hashValue
}
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你 !
编辑:
要强制旋转,请使用以下代码:
let value = UIInterfaceOrientation.LandscapeRight.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)
它适用于iOS 7和8!
Go to your pList and add or remove the following as per your requirement:
"Supported Interface Orientations" - Array
"Portrait (bottom home button)" - String
"Portrait (top home button)" - String
"Supported Interface Orientations (iPad)" - Array
"Portrait (bottom home button)" - String
"Portrait (top home button)" - String
"Landscape (left home button)" - String
"Landscape (right home button)" - String
Run Code Online (Sandbox Code Playgroud)
注意:此方法允许旋转整个应用程序.
要么
在项目中为UIViewControllers创建一个ParentViewController(继承方法).
// UIappViewController.swift
import UIKit
class UIappViewController: UIViewController {
super.viewDidLoad()
}
//Making methods to lock Device orientation.
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
override func shouldAutorotate() -> Bool {
return false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Run Code Online (Sandbox Code Playgroud)
将每个视图控制器的父控制器关联为UIappViewController.
// LoginViewController.swift
import UIKit
import Foundation
class LoginViewController: UIappViewController{
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
Run Code Online (Sandbox Code Playgroud)
对于Swift 3,iOS 10
override open var shouldAutorotate: Bool {
return false
}
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}
Run Code Online (Sandbox Code Playgroud)
但是,有一个错误,该设置shouldAutorotate
目前在iOS 9上不起作用.
归档时间: |
|
查看次数: |
103815 次 |
最近记录: |