1

I am trying to present a view as bottom sheet but it is behaving weirdly while closing the view using drag down. Whenever the keyboard is active it crops the view while dragging down but when keyboard is not active it behaves perfectly. I want to stop this cropping view when dropping down. You can more under stand in the GIFs.

When keyboard is not active [This what I want achieve when keyboard is active]:

When keyboard is not active

When keyboard is active [Focus on edges of sheet] :

enter image description here

I have tried changing method of presenting but using SwiftUIX and iOS 16 sheet modifier. But I have not found the cause of this. And I am not getting any idea why this is happening and yes this behaviour only reproduces in iOS 16.

struct ContentView: View {
    
    @State var presented: Bool = false
    
    var body: some View {
        Button("Show",action: {
            presented.toggle()
        })
        .ignoresSafeArea()
        .sheet(isPresented: $presented) {
            view2
        }
    }
    
    
    private var view2: some View {
        VStack(spacing: 0) {
            TextField(text: .constant("123"))
                .frame(height: 70)
                .background(.gray)
                .padding()
            
            TextField(text: .constant("456"))
                .frame(height: 70)
                .background(.gray)
                .padding()
            
            Spacer()
        }
        .ignoresSafeArea()
        .background(.black)
    }
}
  • 1
    Duplicate from https://stackoverflow.com/q/74019750/17612289 – Mixorok Dec 01 '22 at 08:30
  • Seems like this is different issue. I am facing similar problem and `adaptsKeyboard` does fix those. But it still fails when I have `NavigationView` in my `sheet`. – WhiteSpidy. Dec 08 '22 at 06:43

1 Answers1

0

I don't know why this issue is happening, but I have solved this issue by changing presenting approach.

  1. First reason that making cropping issues is ignoring the safe area using any method will reproduce the same issue. So, you have to remove ignoreSafeArea() or edgesIgnoringSafeArea(). It will solve your problem but there's a chance you have to redesign your screen.

  2. If it will still not work, try presenting the view using ViewController's present method by Creating an object of UIHostingController() by passing your view in it and presenting that UIHostingConrtoller() object.

  3. AdaptToKeyboard() solution in the question comment works but not in every scenario. I had three points that had the same issue adaptsTokeyboard()solved the issue in two points but not o the third point.

Here's example of UIHostingController() approach

extension UIApplication {
    public var firstKeyWindow: UIWindow? {
        windows.first(where: { $0.isKeyWindow })
    }
    
    @available(macCatalystApplicationExtension, unavailable)
    @available(iOSApplicationExtension, unavailable)
    @available(tvOSApplicationExtension, unavailable)
    public var topmostViewController: UIViewController? {
        UIApplication.shared.firstKeyWindow?.rootViewController?.topmostViewController
    }
    
   func present<V: View>(_ view: V) {
       previousTopmostViewController = UIApplication.shared.topmostViewController
       let controller = UIHostingController(rootView: view)
       previousTopmostViewController?.present(controller, animated: true)
   }

}