image - 如何使用 ScrollView Image SwiftUI 删除 Zstack 中的顶部安全区域
问题描述
我正在尝试删除顶部安全区域。有没有办法从顶部和图像中删除顶部安全区域?
代码:-
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)
}
}
带有警报代码的输出
没有警报代码的输出:-
有人可以向我解释如何使用警报代码从图像中删除顶部安全区域,我已尝试按上述方法实施,但还没有结果。
任何帮助将不胜感激。
提前致谢。
解决方案
我删除了您的警报代码。你可以用一个更简单的函数来做同样的事情。
价值
@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()
}
推荐阅读
- f# - 在 F# 中的 Map 上阅读的好材料
- c# - 将主键添加到现有 sqlite 表时,xamarin 应用程序崩溃
- c - 如何确保密码的长度没有限制?
- django - Django REST 框架中的可配置 SAML SSO 身份验证
- python - Anaconda 中的 PyQt5 安装问题
- postgresql - 获取触发触发器的查询字符串
- azure - 使用 power shell 添加密钥保管库访问策略不起作用
- android - Admob 横幅隐藏列表视图片段的最后一项
- python - 如何在熊猫中使用 python 循环替换缺失值?
- r - 使用行名更改数据表中行的背景颜色