I am embedding a "Sign In with Apple" Button in SwiftUI using a UIViewRepresentable struct to display the button.
import SwiftUI
import UIKit
import AuthenticationServices
final class AppleIDButton: UIViewRepresentable {
typealias UIViewType = UIView
var appleButton = ASAuthorizationAppleIDButton()
func makeUIView(context: UIViewRepresentableContext<AppleIDButton>) -> UIView {
let view = UIView()
view.addSubview(appleButton)
return view
}
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<AppleIDButton>) {
appleButton.cornerRadius = 8
}
}
...//Here is the code in my SwiftUI file
ZStack(alignment: .top) {
VStack(spacing: 0) {
Rectangle()
.frame(width: screenWidth, height: 1)
.foregroundColor(Color("lineGray"))
Rectangle()
.frame(width: screenWidth, height: screenHeight * 0.375)
.foregroundColor(.white)
}
VStack {
HStack {
self.appleIDButton
.frame(width: screenWidth - 24, height: 48)
.padding(.leading, 24)
}
.padding(.bottom, 16)
Text("Or")
.font(.system(size: 14))
.foregroundColor(Color("gray"))
.padding(.top, -16)
NavigationLink(destination: SignUpView()) {
HStack {
Spacer()
Text("Sign Up with email")
.font(.system(size: 17))
.fontWeight(.semibold)
.foregroundColor(.white)
Spacer()
}
.padding([.top, .bottom], 12)
}
.background(colors.primaryThemeColor, cornerRadius: 8)
.padding([.leading, .trailing], 24)
.padding(.bottom)
HStack {
Spacer()
Text("Already have an account?")
.foregroundColor(Color("gray"))
NavigationLink(destination: SignInView()) {
Text("Sign In")
.fontWeight(.semibold)
.foregroundColor(colors.primaryThemeColor)
}
Spacer()
}
.font(.system(size: 14))
}
.padding(.top, 24)
}
My code is currently producing a button that is less than half the width of the screen and is touching the leading edge.
When it shows up in the SwiftUI view, How do I change the width of the button to match the right view constraints?
If your interface requires it, you can create a custom Sign in with Apple button for iOS, macOS, or the web.
Use Sign in with Apple You need to be signed in with your Apple ID in System Preferences > Apple ID on your Mac or Settings > [your name] on your iPhone, iPad, or iPod touch.
The Apple logo on the back of an iPhone is actually a hidden shortcut button. There is also a 'Siri Shortcuts' option at the bottom of the list, which displays your shortcuts, if you have any, as options.
iOS Touch Targets In the Human Interface Guidelines, Apple recommends a minimum target size of 44 pixels (px) wide 44 pixels tall. At Transpire we feel that this is definitely a 'minimum size' and in practice this is still too small of an area to be trying to tap successfully.
The default is 15. To configure the width of the Sign in with Apple button, set the data-width property to a value in points between 130—375, or 100% to fit the container size. The default is 100%. To configure the height of the button, set the data-height property to a value in points between 30—64, or 100% to fit the container size.
To configure the appearance of the Sign in with Apple button, set the data-mode property to one of the following values: A center-aligned button. Both the logo and text are centered in the button. This is the default. A left-aligned button with an adjustable logo size, logo position, and label position.
To configure the width of the Sign in with Apple button, set the data-width property to a value in points between 130—375, or 100% to fit the container size. The default is 100%.
By default, the Sign in with Apple button has rounded corners. In iOS, macOS, and the web, you can change the corner radius to produce a button with square corners or a pill-shaped button. For developer guidance, see cornerRadius (iOS and macOS) and Displaying Sign in with Apple buttons on the web.
You can modify the default constraints and set new ones. By default the width needs to be greater than 130.
ASAuthorizationAppleIDButton *appleButton = [[ASAuthorizationAppleIDButton alloc] init];
[appleButton addTarget:self action:@selector(handleSignInWithAppleIDButtonPress:) forControlEvents:UIControlEventTouchUpInside];
// Disable default width constraints
[appleButton.constraints enumerateObjectsUsingBlock:^(__kindof NSLayoutConstraint * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.firstAttribute == NSLayoutAttributeWidth) {
obj.active = NO;
}
}];
// Set new ones
//[appleButton.widthAnchor constraintEqualToConstant:150].active = YES;
[appleButton.widthAnchor constraintEqualToConstant:40].active = YES;
[appleButton.heightAnchor constraintEqualToConstant:40].active = YES;
let appleButton = ASAuthorizationAppleIDButton()
authorizationButton.addTarget(self, action: #selector(handleLogInWithAppleIDButtonPress), for: .touchUpInside)
// Disable default width constraints
appleButton.constraints.forEach { (constraint) in
if (constraint.firstAttribute == .width) {
constraint.isActive = false
}
}
// Set new ones
//appleButton.widthAnchor.constraint(equalToConstant: 150).isActive = true
appleButton.widthAnchor.constraint(equalToConstant: 40).isActive = true
It's a usual process what we tend to miss is
appleSignInButton.translatesAutoresizingMaskIntoConstraints = false
When apple button is Programatically added Auto resizing is enabled and your constraint changes would not respond.
Happy coding
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With