@State, @StateObject, @ObservedObject 關(guān)鍵字
@State 定義在使用它的 View 的最外層,當(dāng)改變時(shí),SwiftUI 刷新依賴它的視圖。@State 只對(duì)當(dāng)前視圖和子視圖可見,屬性的初始化在其生命周期內(nèi)只被賦值一次
@StateObject 配合 ObservableObject 對(duì)象的 @Published 屬性來檢測(cè)屬性的改變。SwiftUI 會(huì)保證 @StateObject 屬性在當(dāng)前視圖生命周期內(nèi)只被改變一次
@ObservedObject
如果視圖的輸入 (參數(shù))是 ObservableObject 對(duì)象(通常是 @StateObject),使用 @ObservedObject 接收它,不要在聲明 @ObservedObject 的視圖里初始化它。用法如下:
class DataModel: ObservableObject {
@Published var name = "Some Name"
@Published var isEnabled = false
}
struct MyView: View {
@StateObject private var model = DataModel()
var body: some View {
Text(model.name)
MySubView(model: model)
}
}
struct MySubView: View {
@ObservedObject var model: DataModel
init(model: DataModel) {
self.model = model // 只應(yīng)該被這樣用
// 不要像以下這樣用,和 State,StateObject 表現(xiàn)不一樣
// self.model = DataMoel() // 此時(shí) model 被重新初始化
// self._model = ObservedObject(wrappedValue: DataModel()) // 此時(shí) model 被重新初始化
}
var body: some View {
Toggle("Enabled", isOn: $model.isEnabled)
}
}
NavigationView
NavigationView {
Text("Row \(number)")
.frame(height: 50).background(
Color.blue
)
.navigationBarHidden(false)
.navigationBarTitle("123", displayMode: .inline)
.background(Color.red)
.background(NavigationConfigurator { nc in
nc.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.blue.withAlphaComponent(calculateOpacity(for: offsetY))]
})
.navigationBarItems(
leading:
Button(action: {
print("Left button tapped")
}) {
Image(systemName: "arrow.left")
}
.buttonStyle(PlainButtonStyle()),
trailing:
HStack {
Button(action: {
print("First button tapped")
}) {
Image(systemName: "plus")
}
.buttonStyle(PlainButtonStyle())
Spacer()
Button(action: {
print("Second button tapped")
}) {
Image(systemName: "plus")
}
.buttonStyle(PlainButtonStyle())
}
)
}
//設(shè)置標(biāo)題顏色
struct NavigationConfigurator: UIViewControllerRepresentable {
var configure: (UINavigationController) -> Void
func makeUIViewController(context: Context) -> UIViewController {
let viewController = UIViewController()
DispatchQueue.main.async {
if let nc = viewController.navigationController {
self.configure(nc)
}
}
return viewController
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}
List 自定義高度
List {
Text("\(isOn)")
.background(Color.clear)
.listRowBackground(Color.clear)
ForEach(0..<5) { number in
HStack {
Text("Row \(number)")
.frame(height: 50).background(
Color.blue
)
Spacer()
Text("Row \(number)")
.frame(height: 50).background(
Color.blue
)
}
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
}
.background(Color.clear)
.listRowBackground(Color.clear)
}
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
.listStyle(.plain)
.navigationBarHidden(false)
.navigationBarTitle("123", displayMode: .inline)
.background(Color.red)
Toggle 開關(guān)選擇器
Toggle("定位開關(guān)", isOn: $isOn).labelsHidden()
.tint(Color.blue)
.toggleStyle(SwitchToggleStyle(tint: Color.red))
.onTapGesture {
if !isOn {
print("111111111111")
} else {
print("222222222222222")
}
}
//一般不通過 onTapGesture點(diǎn)擊來獲取事件,一般通過框架combine 監(jiān)聽isOn 來獲取事件處理
self.$isOn.sink { [weak self] isOn in
guard let self else { return }
}.store(in: &self.cancellables)
Image 加載圖片
// 加載本都圖片
Image("iconName")
Image("iconName", bundle: Resources.resourceBundle) // 文件路徑
//網(wǎng)絡(luò)圖片
AsyncImage(url: URL(string: "https:pic35.photophoto.cn/20150511/0034034892281415_b.jpg"), content: { image in
image.resizable()
}, placeholder: {
Color.red
})
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。