简体   繁体   English

来回选择时锁定的ViewController方向断开

[英]Locked ViewController orientation breaking when segueing back and forth

I have a simple camera app. 我有一个简单的相机应用程序。 The Camera Preview screen is a viewcontroller (name ViewController) which I locked the orientation via this code (can't find the StackOverflow link to this code), The viewcontroller is embedded in a navigation controller: Camera Preview屏幕是一个视图控制器(名称为ViewController),我通过此代码锁定了方向(找不到此代码的StackOverflow链接),该视图控制器嵌入在导航控制器中:

 func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if let navigationController = self.window?.rootViewController as? UINavigationController {
            if navigationController.visibleViewController is ViewController {
                return UIInterfaceOrientationMask.portrait
            } else {
                return UIInterfaceOrientationMask.all
            }
        }
        return UIInterfaceOrientationMask.portrait
    }

When the camera starts up, the Camera Preview is locked in .portrait orientation, which is what I desire. 相机启动时,“相机预览”锁定为.portrait方向,这正是我想要的。 However, if I hit the library button, which segues me to another screen, and change the orientation of the library screen by rotating my physical device, and THEN return back to my Camera Preview screen via the "back" button, the orientation is broken. 但是,如果我按下图库按钮(将我转至另一个屏幕),并通过旋转物理设备来更改图库屏幕的方向,然后通过“后退”按钮返回到“我的相机预览”屏幕,则该方向被破坏了。 。 It's in landscape mode, instead of portrait mode. 它是横向模式,而不是纵向模式。 How do I get a hard lock? 如何获得硬锁?

Just incase, I made a dummy model of my code here (too long to post here), where you guys can test it out yourself 以防万一,我在这里做了一个我的代码的虚拟模型(太长了,无法在此处发布),你们可以在这里自己测试一下

https://github.com/bigmit2011/Testing-Camera https://github.com/bigmit2011/Testing-Camera

EDIT: 编辑:

If the app crashes just try running it one more time. 如果应用程序崩溃,请尝试再运行一次。 For some reason the first time the app is ran it crashes. 由于某种原因,应用程序首次运行时会崩溃。 Thank you. 谢谢。

EDIT2: EDIT2:

Attempting to set embedded ViewController as a delegate of UINavigationController. 尝试将嵌入式ViewController设置为UINavigationController的委托。 However, I don't quite understand if I'm doing it correctly: 但是,我不太了解我是否正确执行了以下操作:

class ViewController: UIViewController, UINavigationControllerDelegate {

    var navigation: UINavigationController?
    self.navigation.delegate = self  // doesn't seem to work


    func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> UIInterfaceOrientationMask {

        return .portrait
    }

Do I need to create a separate swift file for the UiNavigationController? 我是否需要为UiNavigationController创建一个单独的swift文件?

EDIT3: Following Matt's answer code is as follows: EDIT3:以下Matt的答案代码如下:

class CameraViewController :UIViewController, UINavigationControllerDelegate {

override func viewDidLoad() {

    self.navigationController?.delegate = self
    super.viewDidLoad()

func navigationControllerSupportedInterfaceOrientations(_ navigationController: UINavigationController) -> UIInterfaceOrientationMask {

        return .portrait
    }

However, this seems to lock everything in portrait mode not just that single viewController embedded within Navigation Controller. 但是,这似乎将所有内容都锁定为纵向模式,而不仅仅是锁定在Navigation Controller中的单个viewController。

You are making two mistakes. 您正在犯两个错误。

  • First, you are trying to govern the orientation of individual view controllers from the app delegate. 首先,您尝试从应用程序委托中控制各个视图控制器的方向。 That's wrong. 错了 The app delegate governs the totality of possible orientations for the whole app. 应用程序委托人控制整个应用程序的可能方向。 But as for view controllers, each individual view controller governs its own orientation. 但是对于视图控制器,每个单独的视图控制器都控制自己的方向。 Just implement supportedInterfaceOrientations in each of your two view controllers (not in the app delegate). 只需在两个视图控制器(而不是应用程序委托)中的每一个中实现supportedInterfaceOrientations

  • Second, your first view controller is in a navigation interface. 其次,您的第一个视图控制器在导航界面中。 Therefore it is not the top-level view controller and cannot govern orientation directly (with supportedInterfaceOrientations ). 因此,它不是顶级视图控制器,并且不能直接管理方向(使用supportedInterfaceOrientations )。 The correct way to govern the orientation of a navigation interface is to set yourself as the navigation controller's delegate and implement navigationControllerSupportedInterfaceOrientations(_:) . 控制导航界面方向的正确方法是将自己设置为导航控制器的委托,并实现navigationControllerSupportedInterfaceOrientations(_:)

I also couldn't run your app. 我也无法运行您的应用。 But I tested this in Xcode with a main VC, a 2nd VC and embedded the main VC in a navigation controller. 但是我用主VC,第二VC在Xcode中对此进行了测试,并将主VC嵌入到导航控制器中。 I then followed the advice in this article Selective rotation lock in iOS 然后,我按照本文中的建议在iOS中进行了选择。

You add the following to AppDelegate: 您将以下内容添加到AppDelegate:

var enableAllOrientation = false

func application(_ application: UIApplication,supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if (enableAllOrientation == true){
    return UIInterfaceOrientationMask.allButUpsideDown
}
return UIInterfaceOrientationMask.portrait
}

Then in the VC that allows allButUpsideDown (or change this to what you want), you add the following code: 然后在允许allButUpsideDown的VC中(或将其更改为所需的内容),添加以下代码:

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.enableAllOrientation = true
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.enableAllOrientation = false

let value = UIInterfaceOrientation.portrait.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
}

I put this in the 2nd VC. 我将此放在第二个VC中。 Ran the app in the simulator, segued to the 2nd VC, changed the orientation to landscape then hit back and the main VC was locked in portrait. 在模拟器中运行该应用程序,将其锁定到第二个VC,将方向更改为横向,然后回击,然后将主VC锁定为纵向模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 方向锁定的ViewController - ViewController with locked orientation 来回查询时,iOS应用程序堆积内存 - iOS app pilling memory while segueing back and forth 回拨时,UITableViewController单元格仍突出显示 - UITableViewController cell remains highlighted when segueing back 使用UINavigationController iOS 6弹回到初始viewcontroller时,方向会发生变化 - Orientation changes when pop back to the initial viewcontroller using UINavigationController iOS 6 防止 WKWebview 从父 ViewController 来回时重新加载 - Prevent WKWebview from reloading when going back and forth from parent ViewController 锁定方向后跟踪设备方向 - Track device orientation when orientation is locked 当选择回根VC时,UINavigationBar的标题变为空白 - Title of UINavigationBar becomes blank when segueing back to root VC 方向锁定时强制横向 - Force Landscape when orientation is locked 在导航控制器中重新选择视图控制器时,整个屏幕为空白 - When segueing back to view controller in navigation controller, entire screen is blank 切换tabBarController索引而不是隔离时,将数据从一个ViewController传递到另一个ViewController - Passing data from one ViewController to another when switching tabBarController index instead of segueing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM