简体   繁体   English

在键盘处于活动状态时关闭呈现的视图裁剪

[英]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.您可以在 GIF 中了解更多。

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.我尝试更改呈现方法,但使用 SwiftUIX 和 iOS 16 工作表修改器。 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.我不知道为什么会这样,是的,这种行为只在 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().因此,您必须删除 ignoreSafeArea() 或 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.如果它仍然不起作用,请尝试使用 ViewController 的 present 方法呈现视图,方法是通过在其中传递视图并呈现 UIHostingConrtoller() 对象来创建 UIHostingController() 的对象。

  3. AdaptToKeyboard() solution in the question comment works but not in every scenario.问题评论中的 AdaptToKeyboard() 解决方案有效,但并非在所有情况下都有效。 I had three points that had the same issue adaptsTokeyboard()solved the issue in two points but not o the third point.我有三个点有相同的问题 adaptsTokeyboard() 解决了两点的问题,但没有解决第三点。

Here's example of UIHostingController() approach这是 UIHostingController() 方法的示例

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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