-
-
Save phillipcaudell/681bed0aff6a9ffe253e22852c0c8673 to your computer and use it in GitHub Desktop.
List Focus
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
struct RootView: View { | |
@State var items: [Int] = [0,1,2] | |
@State var selected: Int? | |
@State var detailView: Bool = false | |
// 1. Create state | |
@FocusState private var focussed: Bool | |
var body: some View { | |
VStack { | |
if detailView { | |
Button(action: { | |
detailView = false | |
}, label: { | |
Text("Exit") | |
}) | |
} | |
else { | |
List(items, id:\.self, selection: $selected) { index in | |
Text("\(index)") | |
.tag(index) | |
} | |
// 2. Apply modifier | |
.focused($focussed) | |
.onAppear { | |
// 3. Set to true on appear | |
focussed = true | |
} | |
Button(action: { | |
detailView = true | |
}, label: { | |
Text("detail view") | |
}) | |
} | |
} | |
.frame(width: 300, height: 420, alignment: .top) | |
.onAppear { | |
selected = 0 | |
} | |
// Debug | |
.overlay { | |
Text(String(describing: focussed)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment