SwifUI LandScape NavigationTopBar won't hide

136 views Asked by At

I wanted only one landscape View between all Views so I added code bellow to the AppDelegate

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: 
UIWindow?) -> UIInterfaceOrientationMask {
    return AppDelegate.orientationLock
}

and I've created a Custom Modifier like bellow to force specific view to be in landscape mode

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

and the view that I've created is like bellow:

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)

}}

so the problem is after showing this view in landscape mode navigationBar will show and I can't hide it even though I specified it should be hidden..

and here is the rounIntro view which GamePlay present it

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

and the other thing is after pressing navigation bar back button it won't force the view back to portrait mode. here are the pics from the situation: enter image description here

enter image description here

1

There are 1 answers

1
Edison Lo On

You have set the navigation bar for

struct RoundIntro: View {

But what you should set the hidden = true property to respective UIViewController's navigation bar