本节课将创建一个两层嵌套的滚动视图,处于外层的滚动视图允许垂直滚动,而处于内层的滚动视图,只允许水平滚动。
示例代码:
struct ContentView : View { var body: some View { ScrollView(.vertical, showsIndicators: false) { VStack(alignment: HorizontalAlignment.leading, spacing: 20){ Text("Overview") .font(.system(size: 48)) .padding(10) Text("With the power of Xcode, the ease of Swift, and the revolutionary features of cutting-edge Apple technologies, you have the freedom to create your most innovative apps ever.\nSwiftUI provides views, controls, and layout structures for declaring your app's user interface. The framework provides event handlers for delivering taps, gestures, and other types of input to your app, and tools to manage the flow of data from your app's models down to the views and controls that users will see and interact with.") .lineLimit(nil) .frame(width: 300, height: 350, alignment: .topLeading) .padding(10) ScrollView(.horizontal, showsIndicators: true) { HStack(alignment:.center, spacing: 20){ Image("iPhone") .resizable() .frame(width: 189, height: 350, alignment: .center) Image("iPhoneSerial") .resizable() .frame(width: 518, height: 350, alignment: .center) Image("iPhone") .resizable() .frame(width: 189, height: 350, alignment: .center) } } .background(Color.orange) .padding(10) } } .background(Color.orange) .padding(10) .navigationBarTitle(Text("ScrollView")) } }