首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwifUI LandScape NavigationTopBar不会隐藏

SwifUI LandScape NavigationTopBar不会隐藏
EN

Stack Overflow用户
提问于 2020-09-15 16:14:15
回答 1查看 73关注 0票数 1

我想在所有视图之间只有一个横向视图,所以我在AppDelegate中添加了下面的代码

代码语言:javascript
复制
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: 
UIWindow?) -> UIInterfaceOrientationMask {
    return AppDelegate.orientationLock
}

我已经创建了一个像bellow这样的自定义修饰符,以强制特定视图处于横向模式

代码语言:javascript
复制
    struct LandScapeOrientation: ViewModifier {
    
        func body(content: Content) -> some View {
            content
                .onAppear {
                    AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
                    UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
                    UINavigationController.attemptRotationToDeviceOrientation()
                 }
                .onDisappear {
                    DispatchQueue.main.async {
                    AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
                    UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
                    UINavigationController.attemptRotationToDeviceOrientation()
                }
            }
    }}

    extension View {
    func landScape() -> some View{
        self.modifier(LandScapeOrientation())
    }
}

我创建的视图就像下面这样:

代码语言:javascript
复制
struct GameplayScene: View {

//MARK: - Var
@State var round = NeverMindRound.first
@Binding var mode: NevermindMode
//MARK: - Views
private var background: some View {
    colors.customYellowOk.value
        .edgesIgnoringSafeArea(.all)
}
//MARK: - MainBody
var body: some View {
    
   return ZStack{
    
    background
        
    RoundIntro(round: $round)
            .landScape()
    .navigationBarHidden(true)
    .navigationBarBackButtonHidden(true)
    .statusBar(hidden: true)
    }.navigationBarHidden(true)
    .navigationBarBackButtonHidden(true)
    .statusBar(hidden: true)

}}

所以问题是,在以横向模式显示此视图后,navigationBar将会显示,即使我指定它应该隐藏,我也无法隐藏它。

下面是GamePlay提供的rounIntro视图

代码语言:javascript
复制
      struct RoundIntro: View {
    
    
    //MARK: - Vars
    @Binding var round: NeverMindRound
   
    
    //MARK: - View
    private var roundTitle: some View {
        var shadow     = colors.round1TitleShadow.value
        var roundTitle = "Round 1"
        switch round {
            case .first:
                roundTitle = "Round 1"
                shadow     = colors.round1TitleShadow.value
            case .second:
                roundTitle = "Round 2"
                shadow     = colors.round2TitleShadow.value
            case .third:
                roundTitle = "Round 3"
                shadow     = colors.round3TitleShadow.value
        }
        return Text(roundTitle)
                .font(rubik.black.with(Size: 118))
                .foregroundColor(.white)
                .shadow(color: shadow, radius: 6, x: -7, y: 7)
    }
    
    //MARK: - MainBody
    var body: some View {
        roundTitle
        .landScape()
    }
    
    
}

另一件事是在按下导航栏的后退按钮后,它不会强制视图回到纵向模式。以下是该情况的图片:

EN

回答 1

Stack Overflow用户

发布于 2020-09-15 17:07:49

您已将导航栏设置为

代码语言:javascript
复制
struct RoundIntro: View {

但是您应该为相应的UIViewController的导航栏设置hidden = true属性

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63897745

复制
相关文章

相似问题

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