0

我有一个显示 3 个文本字段、3 个按钮和一个切换的警报视图。

当我注释掉前两个时,我只能显示第三个文本字段。

如果我注释掉第 3 个 TextField,则警报视图会使用前 2 个文本字段加载。

我尝试将“组”添加到前 4 个左右的视图元素中,但我认为我没有超过 10 个最大文本字段、按钮等。

我还将警报视图的高度属性增加到 1200,但没有任何区别。

import SwiftUI
import Combine
import SwiftGoogleTranslate

struct AddThingAlert: View {

    @Binding var showingModal: Bool
    @State private var scale: CGFloat = 0.1
    @State private var nativeThing = ""
    @State private var foreignThing = ""
    @State private var conversationName = ""
    @State private var yOffset = 0
    @State private var buttonDisabled = true
    @State private var transliteration = true
    @State private var autoTranslate = true
    
    var language: Language
    var titleText = ""
    var nativePlaceholderText = ""
    var foreignPlaceholderText = ""
    var conversationNamePlaceholderText = ""
    var bodyText = ""
    var buttonText = "Ok"
    var action: (String, String, String) -> Void
    
    @Environment(\.colorScheme) var colorScheme

    @ObservedObject var translate = Translate()

    var body: some View {
        ZStack {
            Color.secondary.opacity(0.4).edgesIgnoringSafeArea(.all)
            VStack(spacing: 0) {
                Text(titleText).bold().padding(.top)
                Text(bodyText).font(.subheadline)
                    .lineLimit(5)
                    .padding(.bottom)
                    .padding(.horizontal)
                    .padding(.top, 5)
                TextField(nativePlaceholderText, text: $nativeThing)
                    .onReceive(Just(nativeThing)) { nativeThing in
                        if self.nativeThing.count < 55 && self.foreignThing.count < 55 && !self.nativeThing.isEmpty && !self.foreignThing.isEmpty {
                            self.nativeThing = self.nativeThing.replacingOccurrences(of: "\\Ufffc", with: "", options: NSString.CompareOptions.literal, range: nil)
                            buttonDisabled = false
                        } else { buttonDisabled = true }
                    }
                    .padding(8)
                    .multilineTextAlignment(.center)
                    .overlay(
                        RoundedRectangle(cornerRadius: 6)
                            .stroke(Color(UIColor.systemGray4), lineWidth: 1)
                    )
                    .font(.subheadline)
                    .padding(.horizontal, 30)
                    .padding(.bottom)
                Button(action: {
                    doAutoTranslate()
                }) {
                    Text("Auto Translate")
                        .foregroundColor(.blue)
                        .cornerRadius(40)
                        .frame(minWidth: 0, maxWidth: .infinity)
                        .font(.system(size: 18))
                        .padding()
                }
                .overlay(
                    RoundedRectangle(cornerRadius: 6)
                        .stroke(Color(UIColor.systemBlue), lineWidth: 1)
                )
                .padding(.horizontal, 50)
                .padding(.bottom)
                TextField(foreignPlaceholderText, text: $foreignThing)
                    .onReceive(Just(foreignThing)) { foreignThing in
                        if self.nativeThing.count < 55 && self.foreignThing.count < 55 && !self.nativeThing.isEmpty && !self.foreignThing.isEmpty {
                            buttonDisabled = false
                        } else { buttonDisabled = true }
                    }
                    .padding(8)
                    .multilineTextAlignment(.center)
                    .overlay(
                        RoundedRectangle(cornerRadius: 6)
                            .stroke(Color(UIColor.systemGray4), lineWidth: 1)
                    )
                    .font(.subheadline)
                    .padding(.horizontal, 30)
                    .padding(.bottom)
                Toggle("Use Transliteration", isOn: $transliteration)
                    .padding(.bottom)
                    .frame(width: 215, height: 50, alignment: .center)
                if transliteration {

                }
                TextField(conversationNamePlaceholderText, text: $conversationName)
                    .onReceive(Just(conversationName)) { conversationName in
                        if self.conversationName.count < 21 {
                            self.conversationName = self.conversationName.replacingOccurrences(of: "\\Ufffc", with: "", options: NSString.CompareOptions.literal, range: nil)
                            buttonDisabled = false
                        } else { buttonDisabled = true }
                    }
                    .padding(8)
                    .multilineTextAlignment(.center)
                    .overlay(
                        RoundedRectangle(cornerRadius: 6)
                            .stroke(Color(UIColor.systemGray4), lineWidth: 1)
                    )
                    .font(.subheadline)
                    .padding(.horizontal, 30)
                    .padding(.bottom)
                Divider()
                HStack(spacing: 0) {
                    HStack {
                        Spacer()
                        Button(action: { self.showingModal.toggle() } ) { Text("Cancel")}
                        Spacer()
                    }
                    Divider()
                    HStack {
                        Spacer()
                        Button(action: self.returnTextFieldInput) { Text("Submit") }
                        .padding()
                        .disabled(buttonDisabled)
                        Spacer()
                    }
                }.frame(height: 50)
            }
            .background(colorScheme == .light ?  Color.white : Color.black)
            .clipShape(RoundedRectangle(cornerRadius: 15))
            .frame(width: 300, height: 700, alignment: .center)
            .scaleEffect(scale)
            .padding(50)
            .onAppear {
                withAnimation(.interpolatingSpring(mass: 0.1, stiffness: 100, damping: 10, initialVelocity: 50)) { self.scale = 1 }
            }
            .offset(CGSize(width: 0, height: self.yOffset))
        }
        .onReceive(NotificationCenter.default.publisher(for: UIApplication.keyboardWillShowNotification)) { _ in
            withAnimation { self.yOffset = 0 }
        }
    }
        
    func returnTextFieldInput() {
        self.action(nativeThing, foreignThing, conversationName)
        self.showingModal.toggle()
    }
    
    func doAutoTranslate() {
        SwiftGoogleTranslate.shared.start(with: "AIzaSyBP_h44xis6jGUdNRZZ2KFQhBwbIUwSD1U")
        let languageLocale = Locale.current.languageCode?.components(separatedBy: "-").first ?? "en"
        let foreignLangaugeCode = changeKeyboard.currentKeyboardLanguage(currentLanguage: language.name!).components(separatedBy: "-").first ?? "en"
        SwiftGoogleTranslate.shared.translate(nativeThing, foreignLangaugeCode, languageLocale) { (text, error) in
            if let _ = text {
                self.foreignThing = text!
            }
        }
    }
}

以及警报视图的接收者:

if addThingIsPresented {
            AddThingAlert(showingModal: self.$addThingIsPresented, language: language,
            titleText: "Add \(language.name ?? "No Language")", nativePlaceholderText:        
            "Enter \(languageLocale.getLanguage(code: Locale.current.languageCode!)) ", 
            foreignPlaceholderText: "Enter \(language.name ?? "No Language")", 
            conversationNamePlaceholderText: "Give name, if conversation...", bodyText:       
            "E.g: Hello", action: { nativeThing, foreignThing, conversationName in

    let native = nativeThing.trimmingCharacters(in: .whitespacesAndNewlines)
    let foreign = foreignThing.trimmingCharacters(in: .whitespacesAndNewlines)
    let conversation = conversationName.trimmingCharacters(in: .whitespacesAndNewlines)

    guard nativeThing.count > 0 && foreignThing.count > 0 else { return }
}
4

0 回答 0