【发布时间】:2020-09-30 05:17:27
【问题描述】:
我有一个导航列表,每个列表项都采用这种格式:
HStack {
TextField("Insert something here.",text: self.$userData.pages[i].title)
.border(Color.blue)
Spacer()
}
这导致以下视图:
The touchable area is highlighted by the blue border and it takes the whole width of the row
这样做的问题是,尽管列表项是一个导航链接,但用户单击该项的任何位置都会导致他们编辑文本内容。我更喜欢的是与Text 具有相同宽度的TextField:
The blue border wraps the text instead of taking the max width
因此,如果用户在TextField 之外单击,导航将起作用,但如果他们在文本上单击,它将让他们编辑文本。 (上面的视图带有Text字段)。
如果我问了一个不清楚或不好的问题,我们深表歉意。我是 Stack Overflow 和 SwiftUI 的新手。
编辑:
我尝试使用fixedSize 修饰符,TextField 正确包装了我的文本,但现在导航链接不起作用(即单击它只是无法导航)。这是我的完整代码:
NavigationLink(destination: PageView(page: self.userData.pages[i])) {
HStack {
Button(action: {}){
TextField(" ", text: self.$userData.pages[i].title)
.fixedSize()
}
.buttonStyle(MyButtonStyle())
.border(Color.blue)
Spacer()
}
}
【问题讨论】:
-
@Asperi 我已经尝试过了,我相信它遇到了我在编辑中描述的相同问题。
标签: swift swiftui swiftui-navigationlink