在SwiftUI中,要在添加新项视图中显示选定的ScrollView项,你可以按照以下步骤进行操作:
以下是一个示例代码,演示如何在添加新项视图中显示选定的ScrollView项:
import SwiftUI
struct ContentView: View {
@State private var selectedItem: Int = 0
var body: some View {
ScrollView {
VStack {
ForEach(0..<10) { index in
Button(action: {
selectedItem = index
}) {
Text("Item \(index)")
.padding()
.foregroundColor(selectedItem == index ? .blue : .black)
}
}
}
}
if selectedItem != -1 {
Text("Selected Item: \(selectedItem)")
.font(.largeTitle)
.padding()
} else {
Text("No item selected")
.font(.largeTitle)
.padding()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
在这个示例中,我们创建了一个ScrollView来显示可滚动的内容,并添加了一个垂直的Stack视图作为内容容器。我们使用ForEach循环来创建多个按钮,每个按钮代表一个ScrollView的可滚动区域。当用户选择一个按钮时,我们更新selectedItem的值来记录选定项的状态。最后,我们根据选定项的状态来显示相应的文本内容。
对于此问题,腾讯云的相关产品和产品介绍链接地址如下:
领取专属 10元无门槛券
手把手带您无忧上云