如何使用 ScrollView Image SwiftUI 删除 Zstack 中的顶部安全区域

How to remove Top Safe Area in Zstack With ScrollView Image SwiftUI

我正在尝试删除顶部安全区域。有什么方法可以从顶部和图像中删除顶部安全区域?

代码:-

struct ContentView22: View {
@State private var showDialog = false
var body: some View {
    ZStack {
        ScrollView {
            VStack {
                Image("CentrImg.jpeg")
                    .resizable()
                    .scaledToFill()
                    .frame(width:UIScreen.screenWidth,height: 180, alignment: .center)
                    .clipped()
                    .ignoresSafeArea()
                    .edgesIgnoringSafeArea(.top)
                VStack(alignment:.leading,spacing:25) {
                    Text("Some text")
                        .onTapGesture {
                            showDialog = true
                        }
                }
            }
        }
        .alert(isPresented: $showDialog,TextAlert(title: "Title",message: "Message") { result in
            print(result as Any)
            if let _ = result {
            } else {
            }
        })
    }.edgesIgnoringSafeArea(.top)
    .background(Color.red)
    .foregroundColor(.white)
    .navigationBarHidden(true)
    .edgesIgnoringSafeArea(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
    .navigationBarBackButtonHidden(true)
    .navigationBarTitle("", displayMode: .inline)
   }
}

警报控件Class:-

import SwiftUI

import Combine

public struct TextAlert {
public var title: String // Title of the dialog
public var message: String // Dialog message
public var placeholder: String = "" // Placeholder text for the TextField
public var accept: String = "OK" // The left-most button label
public var cancel: String? = "Cancel" // The optional cancel (right-most) button label
public var secondaryActionTitle: String? = nil // The optional center button label
public var action: (String?) -> Void // Triggers when either of the two buttons closes the dialog
public var secondaryAction: (() -> Void)? = nil // Triggers when the optional center button is tapped
 }
 extension UIAlertController {
convenience init(alert: TextAlert) {
    self.init(title: alert.title, message: alert.message, preferredStyle: .alert)
    addTextField {
        [=11=].placeholder = alert.placeholder
        [=11=].returnKeyType = .done
    }
    if let cancel = alert.cancel {
        addAction(UIAlertAction(title: cancel, style: .cancel) { _ in
            alert.action(nil)
        })
    }
    if let secondaryActionTitle = alert.secondaryActionTitle {
        addAction(UIAlertAction(title: secondaryActionTitle, style: .default, handler: { _ in
            alert.secondaryAction?()
        }))
    }
    let textField = self.textFields?.first
    addAction(UIAlertAction(title: alert.accept, style: .default) { _ in
        alert.action(textField?.text)
    })
  }
 }
struct AlertWrapper<Content: View>: UIViewControllerRepresentable {
@Binding var isPresented: Bool
let alert: TextAlert
let content: Content

func makeUIViewController(context: UIViewControllerRepresentableContext<AlertWrapper>) -> UIHostingController<Content> {
    UIHostingController(rootView: content)
}

final class Coordinator {
    var alertController: UIAlertController?
    init(_ controller: UIAlertController? = nil) {
        self.alertController = controller
    }
}

func makeCoordinator() -> Coordinator {
    return Coordinator()
}

func updateUIViewController(_ uiViewController: UIHostingController<Content>, context: UIViewControllerRepresentableContext<AlertWrapper>) {
    uiViewController.rootView = content
    if isPresented && uiViewController.presentedViewController == nil {
        var alert = self.alert
        alert.action = {
            self.isPresented = false
            self.alert.action([=11=])
        }
        context.coordinator.alertController = UIAlertController(alert: alert)
        uiViewController.present(context.coordinator.alertController!, animated: true)
    }
    if !isPresented && uiViewController.presentedViewController == context.coordinator.alertController {
        uiViewController.dismiss(animated: true)
      }
    }
  }

 extension View {
public func alert(isPresented: Binding<Bool>, _ alert: TextAlert) -> some View {
    AlertWrapper(isPresented: isPresented, alert: alert, content: self)
  }
}

带警报代码的输出

没有警报代码的输出:-

有人可以向我解释如何使用警报代码从图像中删除顶部安全区域,我已经尝试通过上面的方法实现但还没有结果。

如有任何帮助,我们将不胜感激。

提前致谢。

我删除了您的警报代码。你可以用一个更简单的函数来做同样的事情。

@State var testText: String = ""

警报功能

func alertView() {
        let alert = UIAlertController(title: "Test", message: "Test Message", preferredStyle: .alert)
        alert.addTextField { (testTextField) in
            testTextField.placeholder = "Test TextField"
        }
        
        let okButton = UIAlertAction(title: "OK", style: .default) { (_) in
            self.testText = alert.textFields?[0].text ?? ""
        }
        
        let cancellButton = UIAlertAction(title: "Cancel", style: .destructive) { (_) in
            
        }
        
        alert.addAction(okButton)
        alert.addAction(cancellButton)
        
        UIApplication.shared.windows.first?.rootViewController?.present(alert, animated: true, completion: {
            
        })
    }

使用:

Text("Some text")
    .onTapGesture {
        alertView()
    }