Создайте один файл FloatingInputView.swift. Добавьте к нему код ниже.
import SwiftUI
struct FloatingInputView: View {
@Binding var text: String
@State var placeHolder: String = ""
@State var validationMessage: String = ""
@Binding var error: Bool
@State var optional: Bool = false
@State var keyboardType:UIKeyboardType = .default
let onChange:((String)-> Void?)
var body: some View {
VStack(alignment: .leading, spacing: 5) {
VStack(alignment: .leading, spacing: 0) {
if (!text.isEmpty) {
Text(!text.isEmpty ? placeHolder:"")
.padding(.top, 8)
.padding(.bottom, 0)
.padding(.leading, 15)
.foregroundColor(.white)
.multilineTextAlignment(.leading)
.font(.default, size: 10))
}
TextField("", text: $text)
.placeholder(when: text.isEmpty) {
Text("\(placeHolder)\(optional ? "":"*")").foregroundColor(.gray)
.font(.default, size: 16.0))
}
.keyboardType(keyboardType)
.padding(.all, 5)
.padding(.top, 0)
.padding(.leading, 10)
.font(.default, size: 16.0))
.foregroundColor(.white)
.onChange(of: text) { newValue in
onChange(newValue)
}
}
.frame(height: 60)
.background(.primaryBackground)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(!error ? .orange:.white, lineWidth: 1)
)
.cornerRadius(10)
.shadow(color: .white, radius: 1, x: 0, y: 0)
Text(!error ? validationMessage:"")
.padding(.bottom, 5)
.padding(.leading, 5)
.foregroundColor(.orange)
.multilineTextAlignment(.leading)
.font(.default, size: 10))
}
}
}
представление расширения {
заполнитель функции(
когда следует показывать: Bool,
alignment: Alignment = .leading,
@ViewBuilder заполнитель: () -> Content) -> some View {
ZStack(alignment: alignment) {
placeholder().opacity(shouldShow ? 1 : 0)
self
}
}
// Вызовите этот метод из другого класса
FloatingInputView(text:$firstName, placeHolder: "First Name", validationMessage:"First name required*", error: $isValidFirstname) { newValue in
firstName = newValue
isValidFirstname = !firstName.isEmpty
}
Подробнее здесь: https://stackoverflow.com/questions/785 ... n-swift-ui
Плавающее поле ввода или текстовое поле в пользовательском интерфейсе Swift [закрыто] ⇐ IOS
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение