Last active
April 14, 2024 15:03
-
-
Save michael94ellis/4e877ae9ddecf19b55bbc8b8248ac69f to your computer and use it in GitHub Desktop.
TextField FocusState Demo for iOS 15 SwiftUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Focus State Example | |
// | |
// Created by Michael Robert Ellis on 12/7/21. | |
// | |
import SwiftUI | |
struct MyObject: Identifiable, Equatable { | |
var id: String | |
public var value: String | |
init(name: String, value: String) { | |
self.id = name | |
self.value = value | |
} | |
} | |
struct ContentView: View { | |
@State var myObjects: [MyObject] = [ | |
MyObject(name: "aa", value: "1"), | |
MyObject(name: "bb", value: "2"), | |
MyObject(name: "cc", value: "3"), | |
MyObject(name: "dd", value: "4") | |
] | |
@State var focus: MyObject? | |
var body: some View { | |
ScrollView(.vertical) { | |
VStack { | |
Text("Header") | |
ForEach(self.myObjects) { obj in | |
Divider() | |
FocusField(displayObject: obj, focus: $focus, nextFocus: { | |
guard let index = self.myObjects.firstIndex(of: $0) else { | |
return | |
} | |
self.focus = myObjects.indices.contains(index + 1) ? myObjects[index + 1] : nil | |
}) | |
} | |
Divider() | |
Text("Footer") | |
} | |
} | |
} | |
} | |
struct FocusField: View { | |
@State var displayObject: MyObject | |
@FocusState var isFocused: Bool | |
@Binding var focus: MyObject? | |
var nextFocus: (MyObject) -> Void | |
var body: some View { | |
TextField("Test", text: $displayObject.value) | |
.onChange(of: focus, perform: { newValue in | |
self.isFocused = newValue == displayObject | |
}) | |
.focused(self.$isFocused) | |
.submitLabel(.next) | |
.onSubmit { | |
self.nextFocus(displayObject) | |
} | |
} | |
} |
Author
michael94ellis
commented
Apr 14, 2024
via email
I made this for work a while back and haven’t needed it since, but I’d be
happy
To include it and your credits in the gist if you want.
…On Sun, Apr 14, 2024 at 10:48 AM André J ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
A general purpose solution to this is to use a ViewModifier and FocusState
that use UUID or Int.
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/michael94ellis/4e877ae9ddecf19b55bbc8b8248ac69f#gistcomment-5022935>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGEF4OCYJOW5FPKO6RMLWOLY5KJLVBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTCMZUHEZTCOBYU52HE2LHM5SXFJTDOJSWC5DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment