Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
set size of button from outside
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfum committed Mar 4, 2020
1 parent 6de297d commit 8b20add
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions Sources/SIWA/SIWA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,33 @@ public struct SIWA: View {

private let button: UIControl

public init(button: UIControl = ASAuthorizationAppleIDButton(type: .default, style: .whiteOutline), result: @escaping (Result<Credential, Error>) -> Void) {
public init(
button: UIControl = ASAuthorizationAppleIDButton(type: .default, style: .whiteOutline),
result: @escaping (Result<Credential, Error>) -> Void
) {
self.button = button
self.result = result
}

public var body: some View {
let controller = Controller(credentialResult: result, button: button)
return controller
.frame(width: controller.button.frame.width, height: controller.button.frame.height, alignment: .center)
Controller(credentialResult: result, button: button)
}

private struct Controller: UIViewControllerRepresentable {
private struct Controller: UIViewRepresentable {

let credentialResult: (Result<Credential, Error>) -> Void

let button: UIControl

let vc: UIViewController = UIViewController()

func makeCoordinator() -> Coordinator {
return Coordinator(self)
Coordinator(self)
}

func makeUIViewController(context: Context) -> UIViewController {
vc.view.addSubview(button)
return vc
func makeUIView(context: Context) -> UIControl {
button
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) { }
func updateUIView(_ uiView: UIControl, context: UIViewRepresentableContext<SIWA.Controller>) { }

final class Coordinator: NSObject, ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding {

Expand Down Expand Up @@ -75,7 +73,7 @@ public struct SIWA: View {
}

func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
parent.vc.view.window!
parent.button.window!
}

func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
Expand Down Expand Up @@ -145,4 +143,13 @@ public struct SIWA: View {
}
}
}

@available(iOS 13, *)
struct SIWA_Previews: PreviewProvider {
static var previews: some View {
SIWA() { _ in }
.frame(width: 200, height: 40, alignment: .center)
}
}

#endif

0 comments on commit 8b20add

Please sign in to comment.