swiftUI - 强制屏幕横向后无法关闭模式屏幕

标签 swiftui ios14 xcode12 swiftui-navigationview

我打开一个完整的模态视图

.fullScreenCover(isPresented: self.$isPresentedPlayerView){
     NavigationLazyView((MainPlayerView(playerVM: PlayerVM(asset: self.mediaVM.asset), showModal: self.$isPresentedPlayerView)))
}

在playerView .onApper中我强制屏幕为横向模式 使用此代码:

   func forceLandscapeLeftPlayerView(){
       AppDelegate.orientationLock = UIInterfaceOrientationMask.landscape
       UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
       UINavigationController.attemptRotationToDeviceOrientation()
   }

当它尝试关闭 View 或将 isPresentedPlayerView 设置为 false 或通过presentationMode.wrappedValue.dismiss() 屏幕未关闭! 有什么想法吗???

这是关闭代码:

func closeView(){
    DispatchQueue.main.async {
        withAnimation{
            self.playerVM.pause()
            self.playerVM.destropyPlayer()
            AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
            UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
            UINavigationController.attemptRotationToDeviceOrientation()
            self.isPresentedPlayerView = false
        }
    }
}

顺便说一句,此代码在 Xcode 12.2 上工作,在 Xcode 12.3/.4 上停止工作

最佳答案

这段代码有效。我删除了 DispatchQueuewithAnimation 和前三行。也许,问题出在其他地方。

struct MainPlayerView: View {
    @Environment(\.presentationMode) var presentationMode
    
    
    var body: some View {
        NavigationView {
            Button(action: {
                self.resetOrientation()
                presentationMode.wrappedValue.dismiss()
            }, label: {
                Text("Click")
            })
        }
        .onAppear(perform: {
            UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
            UINavigationController.attemptRotationToDeviceOrientation()
        })
    }
    
    func resetOrientation() {
        UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
        UINavigationController.attemptRotationToDeviceOrientation()
    }
}




关于swiftUI - 强制屏幕横向后无法关闭模式屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65990039/

相关文章:

swift - 更改 TabView 指示器 SwiftUI

ios - 由于在 iOS 14.2 模拟器的 Excluded Architectures 中设置 arm64 导致的大量错误

swift - Xcode 12和SwiftUI : Cannot preview in this file — Failed to update preview

ios - 如何使用包含具有某些布局的 ZStack 的 navigationLink 导航到详细信息屏幕

facebook - SwiftUI Facebook SDK : App ID not found. 将带有您的应用程序 ID 的字符串值添加到 Info.plist

ios - Cordova IOS 应用程序在 IOS 14 中显示白屏

xcode11 - iOS 14 启动屏幕 Storyboard 不会在 iOS 14 设备上显示图像

ios - 日期选择器看起来像文本字段的输入一样被压扁

swiftui - 有人可以使用 SwiftUI+MapKit+LongPress 吗?

swift - 如何在 SwiftUI 中动态隐藏导航后退按钮