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

723 views Asked by At

I'm trying to remove top safe area. Is there any way to remove top safe area from top and image?

Code:-

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

Alert Control 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 {
        $0.placeholder = alert.placeholder
        $0.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($0)
        }
        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)
  }
}

Output with alert code

Screen Shot

Output without alert code:-

enter image description here

Can someone please explain to me how to remove top safe area from image with alert code, I've tried to implement by above but no results yet.

Any help would be greatly appreciated.

Thanks in advance.

1

There are 1 answers

0
Ufuk Köşker On BEST ANSWER

I removed your Alert code. You can do the same with a much simpler function.

Value

@State var testText: String = ""

Alert Func

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

Using:

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