(1)据说在iPad上可以,但手机上不可以
//
// ContentView.swift
// note_test
//
// Created by Gu on 2020/3/7.
// Copyright © 2020 Gu. All rights reserved.
//
import SwiftUI
struct ContentView: View {
@State private var showAlert = false
var body: some View {
VStack {
Button(action: {
let alertHC = UIHostingController(rootView: MyAlert())
alertHC.preferredContentSize = CGSize(width: 300, height: 200)
alertHC.modalPresentationStyle = UIModalPresentationStyle.formSheet
UIApplication.shared.windows[0].rootViewController?.present(alertHC, animated: true)
}) {
Text("click me")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct MyAlert: View {
@State private var text: String = ""
var body: some View {
VStack {
Text("Enter Input").font(.headline).padding()
TextField("写点什么吧", text: $text).padding()
// TextField($text, placeholder: Text("Type text here")).textFieldStyle(.roundedBorder).padding()
Divider()
HStack {
Spacer()
Button(action: {
UIApplication.shared.windows[0].rootViewController?.dismiss(animated: true, completion: {})
}) {
Text("Done")
}
Spacer()
Divider()
Spacer()
Button(action: {
UIApplication.shared.windows[0].rootViewController?.dismiss(animated: true, completion: {})
}) {
Text("Cancel")
}
Spacer()
}.padding(0)
}.background(Color(white: 0.9))
}
}
(2)SwiftUI带有TextField的Alert,大功告成,在Button的action中调用alert()即可。
//
// TextFieldAlertView.swift
// note_test
//
// Created by Gu on 2020/3/8.
// Copyright © 2020 Gu. All rights reserved.
//
import Foundation
import UIKit
//该方法可自定义,只需生成UIAlertController对象,调用showAlert(alert)即可
)即可
func alert() {
let alert = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
alert.addTextField() { textField in
textField.placeholder = "Enter some text"
}
alert.addAction(UIAlertAction(title: "取消", style: .cancel) { _ in})
alert.addAction(UIAlertAction(title: "确定", style: .default) { _ in})
showAlert(alert: alert)
}
func showAlert(alert: UIAlertController) {
if let controller = topMostViewController() {
controller.present(alert, animated: true)
}
}
private func keyWindow() -> UIWindow? {
return UIApplication.shared.connectedScenes
.filter{ $0.activationState == .foregroundActive}
.compactMap{$0 as? UIWindowScene}
.first?.windows.filter {$0.isKeyWindow}.first
}
private func topMostViewController() -> UIViewController? {
guard let rootController = keyWindow()?.rootViewController else {
return nil
}
return topMostViewController(for: rootController)
}
private func topMostViewController(for controller: UIViewController) -> UIViewController {
if let presentedController = controller.presentedViewController {
return topMostViewController(for: presentedController)
} else if let navigationController = controller as? UINavigationController {
guard let topControlelr = navigationController.topViewController else {
return navigationController
}
return topMostViewController(for: topControlelr)
} else if let tabController = controller as? UITabBarController {
guard let topController = tabController as? UITabBarController else {
return tabController
}
return topMostViewController(for: tabController)
}
return controller
}