简体   繁体   中英

Dismissing Presented View Crops while keyboard is active

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 active [Focus on edges of sheet]:

在此处输入图像描述

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM