如何使用ScrollView Image SwiftUI删除Zstack中的顶部安全区域
我正试图移除最安全的区域。有没有办法从顶部和图像中移除顶部安全区域 代码:-如何使用ScrollView Image SwiftUI删除Zstack中的顶部安全区域,image,swiftui,alert,safeareaview,Image,Swiftui,Alert,Safeareaview,我正试图移除最安全的区域。有没有办法从顶部和图像中移除顶部安全区域 代码:- struct ContentView22: View { @State private var showDialog = false var body: some View { ZStack { ScrollView { VStack { Image("CentrImg.jpeg")
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)
}
}
警报控制类:-
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)
}
}
导入快捷界面
进口联合收割机
公共结构文本警报{
公共变量标题:字符串//对话框标题
公共变量消息:字符串//对话框消息
公共变量占位符:String=”“//文本字段的占位符文本
public var accept:String=“OK”//最左边的按钮标签
公共变量取消:字符串?=“取消”//可选的取消(最右边)按钮标签
public var secondaryActionTitle:String?=nil//可选的中间按钮标签
公共变量操作:(字符串?->Void//在两个按钮中的任何一个关闭对话框时触发
public var secondaryAction:(()->Void)?=nil//点击可选的中心按钮时触发
}
扩展控制器{
便利初始化(警报:TextAlert){
self.init(标题:alert.title,消息:alert.message,首选样式:。alert)
addTextField{
$0.placeholder=alert.placeholder
$0.returnKeyType=.done
}
如果let cancel=alert.cancel{
addAction(UIAlertAction)(标题:cancel,样式:.cancel){in
警报。行动(无)
})
}
如果让secondaryActionTitle=alert.secondaryActionTitle{
addAction(UIAlertAction)(标题:secondaryActionTitle,样式:。默认值,处理程序:{in
警报。第二个操作?()
}))
}
让textField=self.textFields?首先
addAction(UIAlertAction(标题:alert.accept,样式:.default){in
警报.action(textField?.text)
})
}
}
结构AlertWrapper:UIViewControllerRepresentable{
@显示绑定变量:Bool
let alert:TextAlert
让内容:内容
func makeUIViewController(上下文:UIViewControllerRepresentableContext)->UIHostingController{
UIHostingController(根视图:内容)
}
期末班协调员{
var alertController:UIAlertController?
初始化(u控制器:UIAlertController?=nil){
self.alertController=控制器
}
}
func makeCoordinator()->Coordinator{
返回协调员()
}
func updateUIViewController(uViewController:UIHostingController,上下文:UIViewControllerRepresentableContext){
uiViewController.rootView=内容
如果显示&&uiViewController.presentedViewController==nil{
var alert=自我警报
alert.action={
self.isPresented=false
self.alert.action($0)
}
context.coordinator.alertController=UIAlertController(警报:警报)
uiViewController.present(context.coordinator.alertController!,动画:true)
}
if!isPresented&&uiViewController.presentedViewController==context.coordinator.alertController{
uiViewController.Disclose(动画:true)
}
}
}
扩展视图{
公共函数警报(isPresented:Binding,alert:TextAlert)->一些视图{
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()
}
我测试了你的代码。我对您的代码没有问题。
。edgesIgnoringSafeArea(.top)
应该足够了。您是否将此视图嵌入到其他视图中?@UfukKöşker,好的,但您可以看到我的屏幕截图,其中显示了我要删除的图像和状态栏之间的顶部空间。@PhilipBorbon我已尝试过,但图像和状态栏之间的差异未被itI删除。我现在正在发短信