首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >swiftUI -在将屏幕强制转换为横屏后,无法关闭模式屏幕

swiftUI -在将屏幕强制转换为横屏后,无法关闭模式屏幕
EN

Stack Overflow用户
提问于 2021-02-01 17:21:02
回答 1查看 114关注 0票数 2

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

代码语言:javascript
复制
.fullScreenCover(isPresented: self.$isPresentedPlayerView){
     NavigationLazyView((MainPlayerView(playerVM: PlayerVM(asset: self.mediaVM.asset), showModal: self.$isPresentedPlayerView)))
}

在playerView .onApper中,我使用以下代码强制屏幕进入横向模式:

代码语言:javascript
复制
   func forceLandscapeLeftPlayerView(){
       AppDelegate.orientationLock = UIInterfaceOrientationMask.landscape
       UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
       UINavigationController.attemptRotationToDeviceOrientation()
   }

当它尝试关闭视图时,或者通过将isPresentedPlayerView设置为false或通过presentationMode.wrappedValue.dismiss() screen not close!有什么想法吗?

下面是闭合代码:

代码语言:javascript
复制
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
        }
    }
}

顺便说一句,这段代码在Xcode12.2上工作,在Xcode12.3上停止工作/ .4

EN

回答 1

Stack Overflow用户

发布于 2021-02-01 18:01:52

这段代码可以工作。我删除了DispatchQueuewithAnimation和前三行。也许,问题出在别的地方。

代码语言:javascript
复制
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()
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65990039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档