Anonymous
SwiftUI — добавление кнопки внутри панели предложений автозамены
Сообщение
Anonymous » 28 сен 2024, 05:29
Кто-нибудь знает, можно ли добавить кнопку на панель автоматических предложений? В итоге попробовал приведенный ниже код и смог добавить кнопку поверх клавиатуры. Но смущает наличие API, который позволяет нам изменять строку состояния автозамены.
CustomTextField
Код: Выделить всё
import Foundation
import UIKit
import SwiftUI
struct CustomTextfield: UIViewRepresentable {
@Binding var text: String
var keyType: UIKeyboardType
func makeUIView(context: Context) -> UITextField {
let textfield = UITextField()
textfield.keyboardType = keyType
let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: textfield.frame.size.width, height: 44))
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(textfield.doneButtonTapped(button:)))
toolBar.items = [doneButton]
toolBar.setItems([doneButton], animated: true)
textfield.leftView = toolBar
return textfield
}
func updateUIView(_ uiView: UITextField, context: Context) {
uiView.text = text
}
}
extension UITextField{
@objc func doneButtonTapped(button:UIBarButtonItem) -> Void {
self.resignFirstResponder()
}
}
TestView
Код: Выделить всё
import Foundation
import SwiftUI
struct TestView : View {
@State var text = ""
var body: some View {
CustomTextfield(text: $text, keyType: UIKeyboardType.asciiCapable)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 50)
.overlay(
RoundedRectangle(cornerRadius: 16)
.stroke(Color.blue, lineWidth: 4)
)
}
}
Подробнее здесь:
https://stackoverflow.com/questions/790 ... estion-bar
1727490587
Anonymous
Кто-нибудь знает, можно ли добавить кнопку на панель автоматических предложений? В итоге попробовал приведенный ниже код и смог добавить кнопку поверх клавиатуры. Но смущает наличие API, который позволяет нам изменять строку состояния автозамены. CustomTextField [code]import Foundation import UIKit import SwiftUI struct CustomTextfield: UIViewRepresentable { @Binding var text: String var keyType: UIKeyboardType func makeUIView(context: Context) -> UITextField { let textfield = UITextField() textfield.keyboardType = keyType let toolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: textfield.frame.size.width, height: 44)) let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(textfield.doneButtonTapped(button:))) toolBar.items = [doneButton] toolBar.setItems([doneButton], animated: true) textfield.leftView = toolBar return textfield } func updateUIView(_ uiView: UITextField, context: Context) { uiView.text = text } } extension UITextField{ @objc func doneButtonTapped(button:UIBarButtonItem) -> Void { self.resignFirstResponder() } } [/code] TestView [code]import Foundation import SwiftUI struct TestView : View { @State var text = "" var body: some View { CustomTextfield(text: $text, keyType: UIKeyboardType.asciiCapable) .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 50) .overlay( RoundedRectangle(cornerRadius: 16) .stroke(Color.blue, lineWidth: 4) ) } } [/code] [img]https://i.sstatic.net/pBWRLQ4f.png[/img] Подробнее здесь: [url]https://stackoverflow.com/questions/79033342/swiftui-adding-button-inside-of-autocorrect-suggestion-bar[/url]