Skip to content

Instantly share code, notes, and snippets.

@fredriccliver
Created December 2, 2020 15:37
Show Gist options
  • Save fredriccliver/21e2c1f75825929ae3402be86b705892 to your computer and use it in GitHub Desktop.
Save fredriccliver/21e2c1f75825929ae3402be86b705892 to your computer and use it in GitHub Desktop.
// cf. https://swiftsenpai.com/xcode/asauthorizationappleidbutton-in-storyboard/
import UIKit
import AuthenticationServices
@IBDesignable
class MyAuthorizationAppleIDButton: UIButton {
private var authorizationButton: ASAuthorizationAppleIDButton!
@IBInspectable
var cornerRadius:CGFloat = 6.0
@IBInspectable
var authButtonType: Int = ASAuthorizationAppleIDButton.ButtonType.default.rawValue
@IBInspectable
var authButtonStyle: Int = ASAuthorizationAppleIDButton.Style.black.rawValue
override public func draw(_ rect: CGRect) {
super.draw(rect)
let type = ASAuthorizationAppleIDButton.ButtonType.init(rawValue: authButtonType) ?? .signIn
let style = ASAuthorizationAppleIDButton.Style.init(rawValue: authButtonStyle) ?? .black
authorizationButton = ASAuthorizationAppleIDButton(authorizationButtonType: type,
authorizationButtonStyle: style)
authorizationButton.cornerRadius = cornerRadius
authorizationButton.addTarget(self, action: #selector(authorizationAppleIDButtonTapped(_:)), for: .touchUpInside)
// Show authorizationButton
addSubview(authorizationButton)
// Use auto layout to make authorizationButton follow the MyAuthorizationAppleIDButton's dimension
authorizationButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
authorizationButton.topAnchor.constraint(equalTo: self.topAnchor, constant: 0.0),
authorizationButton.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0.0),
authorizationButton.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: 0.0),
authorizationButton.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0.0),
])
}
@objc func authorizationAppleIDButtonTapped(_ sender: Any) {
// Forward the touch up inside event to MyAuthorizationAppleIDButton
sendActions(for: .touchUpInside)
}
override public init(frame: CGRect) {
super.init(frame: frame)
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
extension MyAuthorizationAppleIDButton{
@available(iOS 13.0, *)
enum ButtonType: Int{
case signIn
case continues
@available(iOS 13.2, *)
case signUp
}
@available(iOS 13.0, *)
enum Style: Int {
case white
case whiteOutline
case black
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment