Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the width of the "Sign In with Apple" button?

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?

like image 255
Gavin Jensen Avatar asked Jul 18 '19 16:07

Gavin Jensen


People also ask

Can I customize Sign in with Apple button?

If your interface requires it, you can create a custom Sign in with Apple button for iOS, macOS, or the web.

Where is the Apple ID button?

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.

Is the Apple logo a button?

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.

How Big Should buttons be iOS?

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.

What is the default size of the sign in with Apple button?

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.

How do I configure the appearance of the sign in with Apple?

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.

How do I configure the width of the sign in button?

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%.

How do I change the shape of the sign in button?

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.


Video Answer


2 Answers

You can modify the default constraints and set new ones. By default the width needs to be greater than 130.

  • Objective - C
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;
  • Swift
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
like image 81
93sauu Avatar answered Oct 25 '22 11:10

93sauu


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

like image 41
Rajesh Avatar answered Oct 25 '22 12:10

Rajesh