SwiftUI UIApplication.shared.isIdleTimerDisabled = true 仅在一个视图上

xyz*_*eee 1 swift swiftui

当只出现一个视图时如何关闭睡眠?

UIApplication.shared.isIdleTimerDisabled = true 阻止屏幕在每个视图上进入睡眠状态。

如何只是在 ContentView ?

struct ContentView: View {
  UIApplication.shared.isIdleTimerDisabled = true


}
Run Code Online (Sandbox Code Playgroud)

Yon*_*nat 6

使用onAppearonDisappear

var body: some View {
    Text("Hi there")
        .onAppear { UIApplication.shared.isIdleTimerDisabled = true }
        .onDisappear { UIApplication.shared.isIdleTimerDisabled = false }
}
Run Code Online (Sandbox Code Playgroud)