SwiftUI中如何移除Alert对话框内的图标?
移除SwiftUI Alert默认图标,仅显示标题、描述和按钮
SwiftUI原生的Alert组件没有直接提供隐藏默认图标的API,要实现只显示标题、描述文本和按钮的效果,你可以用以下两种方案:
方案一:自定义Alert视图
完全自己构建Alert的布局,精准控制显示内容,适配性更强:
// 自定义无图标Alert视图 struct CustomAlertView: View { @Binding var isPresented: Bool let title: String let message: String let buttonTitle: String var body: some View { ZStack { // 半透明背景遮罩,点击可关闭 Color.black.opacity(0.3) .ignoresSafeArea() .onTapGesture { isPresented = false } // Alert主体内容 VStack(spacing: 16) { Text(title) .font(.headline) .multilineTextAlignment(.center) Text(message) .font(.subheadline) .foregroundColor(.secondary) .multilineTextAlignment(.center) Button(buttonTitle) { isPresented = false } .buttonStyle(.borderedProminent) } .padding() .background(Color.white) .cornerRadius(12) .padding(.horizontal, 24) } } } // 替换原Alert的使用示例 struct YourView: View { @State private var showingAlert = false var body: some View { Button { store.send(.sendRequestButtonTapped) showingAlert = true } label: { Text(TR("Send request")) .font(MeditUIKitFontFamily.NotoSans.regular.swiftUIFont(size: 14)) } .buttonStyle(GhostedButtonStyle( ButtonProperties( size: .small, shape: .roundedRectangle, background: .bTransparent, foreground: MeditUIKitAsset.lightPrimaryP600.swiftUIColor ) )) // 用自定义Alert替换原生Alert .overlay { if showingAlert { CustomAlertView( isPresented: $showingAlert, title: TR("Request sent"), message: TR("You will be notified once the lab approves your request."), buttonTitle: "Ok" ) } } } }
方案二:基于UIKit的Alert包装
通过UIViewControllerRepresentable包装UIAlertController,手动隐藏图标:
// 无图标Alert的UIKit包装 struct NoIconAlert: UIViewControllerRepresentable { @Binding var isPresented: Bool let title: String let message: String let buttonTitle: String func makeUIViewController(context: Context) -> UIViewController { UIViewController() } func updateUIViewController(_ uiViewController: UIViewController, context: Context) { guard isPresented, uiViewController.presentedViewController == nil else { return } let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert) alertController.addAction(UIAlertAction(title: buttonTitle, style: .default) { _ in isPresented = false }) // 遍历视图层级隐藏图标 if let alertContentView = alertController.view.subviews.first?.subviews.first?.subviews.first { alertContentView.subviews.forEach { subview in if subview is UIImageView { subview.isHidden = true } } } uiViewController.present(alertController, animated: true) } } // 替换原Alert的使用示例 struct YourView: View { @State private var showingAlert = false var body: some View { Button { store.send(.sendRequestButtonTapped) showingAlert = true } label: { Text(TR("Send request")) .font(MeditUIKitFontFamily.NotoSans.regular.swiftUIFont(size: 14)) } .buttonStyle(GhostedButtonStyle( ButtonProperties( size: .small, shape: .roundedRectangle, background: .bTransparent, foreground: MeditUIKitAsset.lightPrimaryP600.swiftUIColor ) )) // 用包装后的Alert替换原生Alert .background( NoIconAlert( isPresented: $showingAlert, title: TR("Request sent"), message: TR("You will be notified once the lab approves your request."), buttonTitle: "Ok" ) ) } }
方案对比
- 自定义视图方案:完全可控,不受iOS版本更新影响,还能根据需求自定义样式,推荐使用。
- UIKit包装方案:依赖系统Alert的视图层级结构,iOS版本迭代后可能出现失效情况,仅作为临时替代方案。
内容的提问来源于stack exchange,提问作者유채현
相关产品推荐
相关产品推荐

