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

时间:2019-12-26 16:42:04

标签: swift swiftui

仅显示一个视图时如何关闭睡眠?

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

在ContentView中怎么样?

synchronized

1 个答案:

答案 0 :(得分:1)

使用onAppearonDisappear

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