白色或浅色ASAuthorizationAppleIDButton“使用Apple登录”按钮-Swift

时间:2019-11-21 18:44:14

标签: ios swift

我正在按照Apple的要求实现ASAuthorizationAppleIDButton,但是不幸的是,我的登录背景是黑暗的,我找不到如何将按钮的颜色更改为浅色或白色的按钮,就像Apple在许多示例中显示的那样?< / p>

这就是我所拥有的:

let appleButton = ASAuthorizationAppleIDButton()

override func viewDidLoad() {
    super.viewDidLoad()       
    setAppleButtonStyle()
}

func setAppleButtonStyle() {
    if #available(iOS 13.0, *) {
        appleButton.translatesAutoresizingMaskIntoConstraints = false

        view.addSubview(appleButton)
        NSLayoutConstraint.activate([
            appleButton.centerYAnchor.constraint(equalTo: facebookLoginView.centerYAnchor, constant: -70),
            appleButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
            appleButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
            appleButton.heightAnchor.constraint(equalToConstant: 50)
        ])
    } else {
        //Do Nothing
    }

}

4 个答案:

答案 0 :(得分:3)

如果使用的是Objective-C,这将是您要使用的:

/**
 * If available, returns wrapper for `process.hrtime.bigint()` else
 * falls back to `Date.now()`. In all cases, returns a nanosec-scale
 * timestamp, either as `bigint` or `number`.
 */
export const now =
    typeof BigInt !== "undefined"
        ? typeof process !== "undefined" &&
          typeof process.hrtime !== "undefined" &&
          typeof process.hrtime.bigint === "function"
            ? () => process.hrtime.bigint()
            : () => BigInt(Date.now() * 1e6)
        : () => Date.now() * 1e6;

答案 1 :(得分:2)

在实例化按钮实例时仅提供所需的style

例如

let appleButton = ASAuthorizationAppleIDButton(type: .default, style: .white)

答案 2 :(得分:0)

带有 SwiftUI 的 Swift 5.0,自动调整为暗模式

import SwiftUI
import AuthenticationServices

final class AppleSignInButton: UIViewRepresentable {
  @Environment(\.colorScheme) var colorScheme
  
  func makeUIView(context: Context) -> ASAuthorizationAppleIDButton {
    return ASAuthorizationAppleIDButton(type: .default, style: colorScheme == .dark ? .white : .black)
  }
  func updateUIView(_ uiView: ASAuthorizationAppleIDButton, context: Context) {
  }
}

用法:

AppleSignInButton()
  .frame(width: 300, height: 56) // You can change these values
  .onTapGesture {
    // Do log in stuff
  }

答案 3 :(得分:0)

暗模式支持的自动样式:

let button: ASAuthorizationAppleIDButton = ASAuthorizationAppleIDButton(
    authorizationButtonType: .default,
    authorizationButtonStyle: UITraitCollection.current.userInterfaceStyle == .dark ? .white : .black
)