赞
踩
Example Code
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Hello, World!")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
默认情况下,
SwiftUI
视图文件声明了两个结构体 , 第一个结构体, 描述视图的内容和布局。第二个结构声明了该视图的预览。
在Xcode的右侧, 新增了视图预览窗口, 点击Resume可以进行静态预览
显示/关闭预览 Editor > Editor and Canvas
struct ContentView: View {
var body: some View {
Text("Hello, summerxx")
.foregroundColor(.blue)
.font(Font.system(Font.TextStyle.largeTitle))
}
}
struct ContentView: View { var body: some View { VStack { Text("summerxx0") .font(.title) .foregroundColor(.blue) .font(Font.system(Font.TextStyle.largeTitle)) .frame(width: 150, height: 150, alignment: Alignment.leading) HStack { Text("summerxx1") .font(.title) .foregroundColor(.blue) .font(Font.system(Font.TextStyle.largeTitle)) Text("summerxx2") .font(.title) .foregroundColor(.blue) .font(Font.system(Font.TextStyle.largeTitle)) } } } }
上面是简单组合的演示, 同时 你可能需要多种不同的控件添加 - 可以通过Xcode右上角的加号进行, 控件的添加 (常用的有 scrollView, button, list, Form 等等)
Image("summerxx").frame(width: 400.0, height: 400.0).clipShape(Circle())
UIViewRepresentable
然后重写父类方法makeUIView
, updateUIView
struct XTCustomMapView: UIViewRepresentable { func makeUIView(context: Context) -> MKMapView { MKMapView(frame: .zero) } func updateUIView(_ uiView: MKMapView, context: Context) { // 1. 经纬度 let coordinate = CLLocationCoordinate2D( c) // 2. 跨度 let span = MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0) // 3. 地区 let region = MKCoordinateRegion(center: coordinate, span: span) // 4. 设置 uiView.setRegion(region, animated: true) } }
struct ContentView: View { var body: some View { VStack { XTCustomMapView().edgesIgnoringSafeArea(.top) .frame(height: 300) Image("summerxx1").frame(width: 200.0, height: 200.0).clipShape(Circle()) Text("夏天然后") .font(.title) .foregroundColor(.blue) .font(Font.system(Font.TextStyle.largeTitle)) .frame(width: 150, height: 50, alignment: Alignment.center) HStack { Text("个人博客") .font(.title) .foregroundColor(.blue) .font(Font.system(Font.TextStyle.largeTitle)) Text("summerxx.com") .font(.title) .foregroundColor(.blue) .font(Font.system(Font.TextStyle.largeTitle)) } Button(action: /*@START_MENU_TOKEN@*/{}/*@END_MENU_TOKEN@*/) { Text("编辑资料") }.frame(width: 100, height: 40, alignment: Alignment.center) .font(Font.system(size: 18)) } } }
总结: 以上就是包含了SwiftUI的一些简单语法, 以及UI控件的构建, 编译器的一些简单使用
Demo地址 将会持续更新SwiftUI相关代码
https://github.com/summerxx27/SwiftUITutorials
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。