NavigationLink 在 macOS 下除 List 之外的其他设备上是否可以正常运行
有没有人得到了一个 macOS 示例,其中除了 List 之外的任何内容中的 navigationLink 都可以按预期工作?
在一些测试中,我发现如果使用列表,它工作得很好,但是,在 ScrollView、VStack 或 LazyHGrid 中,它只会在每次单击时按预期更新目标。
下面是我的测试代码。 (忽略滑动的东西 - 但感谢 此处了解见解)。如果我使用 List 作为容器,详细信息视图将按预期显示。将列表替换为其他 3 个中的任何一个都会导致每秒单击一次都会生成空白详细信息。 IE 单击第一、第二、第三、第四,它会输出第一、空白、第三、空白。
这只是一个 macOS,“在 Apple 修复它之前一直使用它”还是我错过了一些基本的东西?
蒂亚·
艾伦
import SwiftUI
struct ContentView: View {
enum SwipeHorizontalDirection: String {
case left, right, none
}
@State private var swipeHorizontalDirection:SwipeHorizontalDirection = .none
let rows = Array(repeating: GridItem(.fixed(100)), count: 6)
let tiles = [
TileView( title: "first"),
TileView( title: "second"),
TileView( title: "third"),
TileView( title: "fourth")
]
@State private var selectedTileID: UUID? = nil
var body: some View {
VStack {
Text("A Test View")
NavigationView {
// VStack {
// LazyHGrid(rows: rows) {
// ScrollView {
List {
ForEach (0 ..< tiles.count) { index in
NavigationLink(tiles[index].title, destination: tiles[index], tag: tiles[index].id, selection: $selectedTileID)
}
}
}
}
.gesture(DragGesture()
.onEnded{
if $0.startLocation.x > $0.location.x {
self.swipeHorizontalDirection = .left
} else if $0.startLocation.x == $0.location.x {
self.swipeHorizontalDirection = .none
} else {
self.swipeHorizontalDirection = .right
}
let nextID = nextItemAdjacentActive(direction: swipeHorizontalDirection)
selectedTileID = nextID
}
)
}
func nextItemAdjacentActive(direction: SwipeHorizontalDirection) -> UUID
{
var foundIndex = 0
for index in 0 ..< tiles.count {
if tiles[index].id == selectedTileID {
if direction == .right {
if (index + 1) < tiles.count {
foundIndex = index+1
}
else { foundIndex = 0}
}
else if direction == .left {
if (index > 0) {
foundIndex = index-1
}
else {
foundIndex = tiles.count - 1
}
}
}
}
return tiles[foundIndex].id
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct TileView: View, Identifiable {
var id = UUID()
var title:String
var body: some View {
VStack {
Text(title).font(.largeTitle)
}
}
}
Has anyone got a working macOS example of a navigationLink within anything other than a List that works as expected ?
On some testing, I find that it works fine if it is with a List, however, in ScrollView, VStack or LazyHGrid it only updates the destination as expected on every second click.
Below is my test code. (ignore the swipe stuff - but thanks to here for the insights). If I use List as the container, the detail views appear as expected. Replacing List with either of the other 3 results in every second click producing a blank detail. I.E. click, first, second, third, fourth and it delivers first, blank, third, blank.
Is this just a macOS, "live with until Apple fixes it" or am I missing something fundamental ?
TIA
Alan
import SwiftUI
struct ContentView: View {
enum SwipeHorizontalDirection: String {
case left, right, none
}
@State private var swipeHorizontalDirection:SwipeHorizontalDirection = .none
let rows = Array(repeating: GridItem(.fixed(100)), count: 6)
let tiles = [
TileView( title: "first"),
TileView( title: "second"),
TileView( title: "third"),
TileView( title: "fourth")
]
@State private var selectedTileID: UUID? = nil
var body: some View {
VStack {
Text("A Test View")
NavigationView {
// VStack {
// LazyHGrid(rows: rows) {
// ScrollView {
List {
ForEach (0 ..< tiles.count) { index in
NavigationLink(tiles[index].title, destination: tiles[index], tag: tiles[index].id, selection: $selectedTileID)
}
}
}
}
.gesture(DragGesture()
.onEnded{
if $0.startLocation.x > $0.location.x {
self.swipeHorizontalDirection = .left
} else if $0.startLocation.x == $0.location.x {
self.swipeHorizontalDirection = .none
} else {
self.swipeHorizontalDirection = .right
}
let nextID = nextItemAdjacentActive(direction: swipeHorizontalDirection)
selectedTileID = nextID
}
)
}
func nextItemAdjacentActive(direction: SwipeHorizontalDirection) -> UUID
{
var foundIndex = 0
for index in 0 ..< tiles.count {
if tiles[index].id == selectedTileID {
if direction == .right {
if (index + 1) < tiles.count {
foundIndex = index+1
}
else { foundIndex = 0}
}
else if direction == .left {
if (index > 0) {
foundIndex = index-1
}
else {
foundIndex = tiles.count - 1
}
}
}
}
return tiles[foundIndex].id
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct TileView: View, Identifiable {
var id = UUID()
var title:String
var body: some View {
VStack {
Text(title).font(.largeTitle)
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论