TextFieldAlert

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值