1、Toggle控件:
// Switch
Toggle(isOn: .constant(showFavoriteOnly), label: {
Text("只显示收藏")
})
2、按钮动画:
struct HikeView: View {
@State private var showDetail = false
var body: some View {
Button(action: {
self.showDetail.toggle()
}) {
Image(systemName: "chevron.right.circle")
.imageScale(.large)
.rotationEffect(.degrees(showDetail ? 90 : 0))
// scaleEffect(showDetail ? 1.5 : 1)
.padding()
.animation(.spring())
}
}
}
3、设置图片:
struct Home: View {
var body: some View {
NavigationView {
List {
Image(landmarks[0].imageName)
.resizable()
.scaledToFill()
.frame(height: 200)
.clipped() // 注意设置的顺序问题
.listRowInsets(EdgeInsets()) // 消除间距
}
}
}
}
4、NavigationBarItem:
.navigationTitle("精选")
.navigationBarItems(trailing: Image(systemName: "person.crop.circle")
.imageScale(.large)
.padding())
5、SwiftUI单例的使用:
import Foundation
struct User {
var username: String
var preferNotification = true
var prefersSeason = Season.winter
var birthday = Date()
// 单例
static let `default` = Self(username: "思思")
enum Season: String, CaseIterable {
case spring = "chun"
case summer = "xia"
case autumn = "qiu"
case winter = "dong"
}
}
6、日期格式的转换:
var dateFormatter: DateFormatter {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy年M月d日"
return dateFormatter
}
Text("生日: \(dateFormatter.string(from: profile.birthday))")
Text("生日: \(profile.birthday, formatter: dateFormatter)")
7、SwiftUI转UIViewController:
var page = UIHostingController(rootView: Image(landmarks[0].imageName))
网友评论