本节课演示如何在PreviewProvider中添加导航视图,从而将页面里的元素,可以在预览时显示在导航视图之中。
示例代码:
struct ContentView : View { var body: some View { VStack{ Image("SwiftUI") .navigationBarTitle("About SwiftUI") Text("SwiftUI is an innovative, exceptionally simple way to build user interfaces across all Apple platforms with the power of Swift. Build user interfaces for any Apple device using just one set of tools and APIs. With a declarative Swift syntax that’s easy to read and natural to write, SwiftUI works seamlessly with new Xcode design tools to keep your code and design perfectly in sync. Automatic support for Dynamic Type, Dark Mode, localization, and accessibility means your first line of SwiftUI code is already the most powerful UI code you’ve ever written.") .padding() } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { NavigationView { //将内容视图放置在NavigationView中里,从而预览当前页面位于导航视图里的效果 ContentView() } } }