saz*_*azz 60 landscape ios swift
我试图在我的应用程序中强制只有一个视图在横向模式,我打电话
override func shouldAutorotate() -> Bool {
print("shouldAutorotate")
return false
}
override func supportedInterfaceOrientations() -> Int {
print("supportedInterfaceOrientations")
return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue)
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.LandscapeLeft
}
Run Code Online (Sandbox Code Playgroud)
视图以纵向模式启动,并在更改设备方向时保持旋转.
永远不会调用shouldAutorotate.
任何帮助,将不胜感激.
saz*_*azz 69
它可能对其他人有用,我找到了一种强制视图以横向模式启动的方法:
把它放在viewDidLoad()中:
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)
和,
override var shouldAutorotate: Bool {
return true
}
Run Code Online (Sandbox Code Playgroud)
Man*_*ios 47
override func viewDidLoad() {
super.viewDidLoad()
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeLeft
}
override var shouldAutorotate: Bool {
return true
}
Run Code Online (Sandbox Code Playgroud)
//如果您的视图嵌入在导航控制器中,则仅上述操作无效.你必须级联//所以在类定义后添加以下扩展名
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)
override func viewDidLoad() {
super.viewDidLoad()
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}
private func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.landscapeLeft
}
private func shouldAutorotate() -> Bool {
return true
}
Run Code Online (Sandbox Code Playgroud)
Zee*_*sha 23
Swift 4,在iOS 11中测试过
您可以在projectTarget中指定方向- >常规 - > DeploymentInfo(设备方向) - >纵向(Landscapeleft和Landscaperight是可选的)
AppDelegate中
var myOrientation: UIInterfaceOrientationMask = .portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return myOrientation
}
Run Code Online (Sandbox Code Playgroud)
LandScpaeViewController
override func viewDidLoad() {
super.viewDidLoad()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.myOrientation = .landscape
}
Run Code Online (Sandbox Code Playgroud)
OnDismissButtonTap
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.myOrientation = .portrait
Run Code Online (Sandbox Code Playgroud)
而已.:)
小智 18
使用Swift 2.2
尝试:
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)
其次是:
UIViewController.attemptRotationToDeviceOrientation()
Run Code Online (Sandbox Code Playgroud)
来自Apple的UIViewController类参考:
某些视图控制器可能希望使用特定于应用程序的条件来确定支持哪些接口方向.如果您的视图控制器执行此操作,当这些条件发生更改时,您的应用程序应调用此类方法.系统立即尝试旋转到新方向.
然后,正如其他人建议的那样,根据需要覆盖以下方法:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.LandscapeLeft
}
override func shouldAutorotate() -> Bool {
return true
}
Run Code Online (Sandbox Code Playgroud)
我有一个签名视图的类似问题,这解决了我.
Ela*_*van 15
在 AppDelegate 中添加:
//Orientation Variables
var myOrientation: UIInterfaceOrientationMask = .portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return myOrientation
}
Run Code Online (Sandbox Code Playgroud)
在想要改变方向的 viewController 中添加这个:
override func viewDidLoad() {
super.viewDidLoad()
self.rotateToLandsScapeDevice()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.rotateToPotraitScapeDevice()
}
func rotateToLandsScapeDevice(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.myOrientation = .landscapeLeft
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
UIView.setAnimationsEnabled(true)
}
func rotateToPotraitScapeDevice(){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.myOrientation = .portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
UIView.setAnimationsEnabled(true)
}
Run Code Online (Sandbox Code Playgroud)
Mic*_*bob 10
对我来说,最好的结果来自Zeesha的答案和sazz的答案.
将以下行添加到AppDelegate.swift:
var orientationLock = UIInterfaceOrientationMask.portrait
var myOrientation: UIInterfaceOrientationMask = .portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return myOrientation
}
Run Code Online (Sandbox Code Playgroud)
将以下行添加到视图控制器类:
let appDel = UIApplication.shared.delegate as! AppDelegate
Run Code Online (Sandbox Code Playgroud)
将以下行添加到视图控制器中viewDidLoad()
:
appDel.myOrientation = .landscape
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)
(可选)将此行添加到您的解散功能:
appDel.myOrientation = .portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)
这些代码行的作用是将默认方向设置为纵向,在视图控制器加载时将其旋转,然后在视图控制器关闭后最终将其重置为纵向.
覆盖(在 ViewController 中):
override public var shouldAutorotate: Bool {
return false
}
override public var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .landscapeRight
}
override public var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .landscapeRight
}
Run Code Online (Sandbox Code Playgroud)
ios 13 的提示。从 ios 13 开始,VC 具有不同的modalPresentationStyle
as.automatic
和设备呈现模式视图,而不是 Full-Screen VC。要解决这个问题,必须设置modalPresentationStyle
为.fullScreen
. 例子:
let viewController = YourViewController()
viewController.modalPresentationStyle = .fullScreen
Run Code Online (Sandbox Code Playgroud)
我需要强制一个控制器进入纵向.添加这个对我有用.
iOS 11的swift 4
override var supportedInterfaceOrientations : UIInterfaceOrientationMask{
return .portrait
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
61535 次 |
最近记录: |