I would like to add a TextField to a .contextMenu, something like
.contextMenu {
TextField("type the code", text: $text)
}
I did that and the TextField shows but it is read only.
Is it possible to make the TextField able to accept typing? That would be amazing.
Related
For the SwiftUI Image element, the voiceover template is seems "accessibility label - image - image name", e.g. for
var body: some View {
Image(systemName: "equal")
.accessibilityLabel("my label")
}
I am getting voiceover response "my label image equal".
Is it possible for voiceover to only say "my label", and not pronounce the "image equal" part?
Once the element gets the focus, the default trait(link, button, label, etc) will be played after accessibilityLabel text. That's the reason it reads out as "my label -> image"
To add or remove the default trait following methods can be used :
.accessibilityAddTraits
.accessibilityRemoveTraits
Example
To recognize an image as a button:
Add .isButton trait and remove the .isImage trait, now VoiceOver can read the description of Image as "my label -> button"
struct ContentView: View {
var body: some View {
Image(systemName: "equal")
.accessibilityLabel("my label")
.accessibilityAddTraits(.isButton)
.accessibilityRemoveTraits(.isImage)
}
}
As an element can have multiple traits, remove the ones you don't want the voiceover to read.
One of my packages that I maintain uses editingChanged on TextField() to detect when the field is actually focused and then modify the text. This has worked fine on other iOS versions but in iOS16 it doesn't seem you're able to update a value from within a TextField if it's being used by that TextField
Is this the wrong way to update an #State property and it was bugged before 16 or is this something that was bugged in 16? If it's bugged, no big deal... but if I'm doing something wrong, of course I would like to fix that.
Below is a simplified version of what my package does, remember this was working before 16. You can even load it into a 15.5 sim and a 16 sim and see the difference.
#State private var text = ""
var body: some View {
Form {
TextField("", text: $text) { editingChanged in
text = "Should update text, but doesn't."
}
Button(action: {
text = "This will update without issue."
}) {
Text("Manually update via button.")
}
}
}
Try to use the following:
TextField("", text: $text)
.onChange(of: text) {
text = "should work now"
}
I want to give a text name for this menu but it won't let me saying there's an Extra argument in call what's wrong ? PS: I'm a beginner in coding
Divider()
Menu("\(buttonTitle)" ) {
Button("Riyadh",action: {SelectRiyadh()})
Button("Dammam",action: {SelectDammam()})
Button("Jeddah",action: {SelectJeddah()})
Button("Mecca",action: {SelectMecca()})
}
We need to see the ContentView page, but try to wrap all that code in some group or into stack, for example VStack.
Anyway, read this article first: How to show a menu when a button is pressed
VStack{
Divider()
Menu("\(buttonTitle)" ) {
Button("Riyadh",action: {option()})
}
}
So I made a simple app with simple text field instance bound to a local variable in SwiftUI
TextField("Main task", text: $store.mainTask)
.textFieldStyle(RoundedBorderTextFieldStyle())
.font(Font.custom("SF Pro Display", size: 14))
The text field is wrapped inside a view with transition and animation.
// text field is inside custom View
// conditional rendering—if that matters
if (true) {
CustomView()
.transition(.asymmetric(insertion: AnyTransition.opacity.animation(Animation.easeInOut(duration: 1).delay(0.5)), removal: AnyTransition.opacity.animation(Animation.easeInOut(duration: 0.1))))
}
I distributed the beta. Some people say they experienced the text wobbling as they typed. Some didn't. Does anyone know what's the reason?
I think it might be related with the animation, but I'm not so sure.
I'd like to use a SwiftUI TextField and a SwiftUI List to render a "search box" above a list of items. Something roughly like the search box available in Safari's Help menu item...which provides a search box where you can always enter text while simultaneously browsing through the list of results using the up and down arrow keys.
I've played with onMoveCommand, focusable, and adjustments to the "parent" NSWindow, but haven't found a clear and obvious way for the TextField to constantly accept input while still being able to navigate the underlying List using the up and down arrow keys. The following code allows for either text to be entered in the TextField, or list entries to be navigated through, but not both at the same time...
struct ContentView: View {
#State var text: String = ""
#State var selection: Int? = 1
var body: some View {
VStack {
TextField("Enter text", text: $text)
List(selection: $selection) {
ForEach((1...100), id: \.self) {
Text("\($0)")
}
}
}
}
}
You can wrap a NSTextField in a NSViewRepresentable and use the NSTextFieldDelegate to intercept the move up and down keys. You can take a look into my suggestions demo.
Source of a text field with suggestions