skip to Main Content

I have view that I’d like to show as a fullScreenCover, I noticed one little issue after presenting it that it does not resize the font when the dynamic type changes. Is there a nice way of fixing this?

Screen recording of changing the dynamic type

Here’s my code

import SwiftUI

struct FullScreenModal: View {
    @State var showModal: Bool = false
    var body: some View {
        VStack {
            Text("Parent view")
            Button("Open Modal") {
                showModal = true
            }
        }
        
        .fullScreenCover(isPresented: $showModal, content: {
            Text("This is a full screen cover.")
        })
    }
}

#Preview {
    FullScreenModal()
}

2

Answers


  1. Chosen as BEST ANSWER

    I got inspired by @Martin Poulsen answer, so I want to keep the size categories the same on Parent and Child views, so for that you'd need to get an instance of sizeCategory Environment object and pass that on to the child the same way that Martin has done. Here's the working example:

    import SwiftUI
    
    struct FullScreenModal: View {
        @Environment(.sizeCategory) var sizeCategory
        @State var showModal: Bool = false
        var body: some View {
            VStack {
                Text("Parent view")
                Button("Open Modal") {
                    showModal = true
                }
            }
            
            .fullScreenCover(isPresented: $showModal, content: {
                Text("This is a full screen cover.")
                    .environment(.sizeCategory, sizeCategory)
            })
        }
    }
    
    #Preview {
        FullScreenModal()
    }
    

    enter image description here


  2. Here is an example with a DetailView, and also with only a Text in your sheet. Use .environment(.sizeCategory, .accessibilityExtraExtraExtraLarge)

    import SwiftUI
        
    struct ContentView: View {
        @State var showModal: Bool = false
    
        var body: some View {
            VStack {
                Text("Parent view")
                Button("Open Modal") {
                    showModal = true
                }
            }
            .fullScreenCover(isPresented: $showModal, content: {
    //            DetailView()
                Text("Detail view")
                    .environment(.sizeCategory, .accessibilityExtraExtraExtraLarge)
            })
        }
    }
    
    #Preview {
        ContentView()
            .environment(.sizeCategory, .accessibilityExtraExtraExtraLarge)
    }
    
    #Preview("Detail") {
        DetailView()
            .environment(.sizeCategory, .accessibilityExtraExtraExtraLarge)
    }
    
    struct DetailView: View {
        @Environment(.dismiss) var dismiss
        
        var body: some View {
            Text("This is a full screen cover.")
            Button("Close") {
                dismiss()
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search