繁体   English   中英

SwiftUI SignInWithAppleButton 暗模式

[英]SwiftUI SignInWithAppleButton dark mode

我在 SwiftUI 应用程序中使用SignInWithAppleButton 但是,当我将设备置于暗模式时,按钮不会自动更改为人机界面指南建议的暗背景配色方案。 我可以在不手动定义 colors 的情况下启用该行为吗?

struct AuthenticationView: View {
    
    var authManager = AuthManager()
    
    var body: some View {
        SignInWithAppleButton(
            onRequest: { request in
                authManager.createRequest(request)
            },
            onCompletion: { result in
                authManager.handleResult(result)
            })
            .frame(width: 280, height: 45, alignment: .center)
        }
    }
}

由于某种原因,该按钮不会使用配色方案自动更新,但您可以使用以下代码修复它:

struct AuthenticationView: View {
    @Environment(\.colorScheme) private var colorScheme
    let authManager = AuthManager()

    private var buttonStyle: SignInWithAppleButton.Style {
        switch colorScheme {
        case .light: return .black
        case .dark: return .white
        @unknown default: return .black
        }
    }

    var body: some View {
        SignInWithAppleButton(
            .signIn,
            onRequest: authManager.createRequest,
            onCompletion: authManager.handleResult
        )
        .signInWithAppleButtonStyle(buttonStyle)
        .id(colorScheme)
        .frame(height: 56)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM