【问题标题】:swiftUI - can't close modal screen after force screen to LandscapeswiftUI - 强制屏幕到横向后无法关闭模式屏幕
【发布时间】:2021-05-05 11:20:07
【问题描述】:

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

.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()
   }

当它尝试关闭视图或将 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 上停止工作

【问题讨论】:

    标签: swiftui ios14 xcode12 swiftui-navigationview


    【解决方案1】:

    此代码有效。我删除了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()
        }
    }
    
    
    
    
    

    【讨论】:

      猜你喜欢
      • 2014-08-10
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 2020-04-06
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      相关资源
      最近更新 更多