Thursday, April 28, 2016

Google Sign-In for iOS - Create a custom sign-in button programmatically

The GIDSignInButton can be assigned as one of the following styles (See GIDSignInButton Class):

GIDSignInButtonStyle.Standard => 230x48


GIDSignInButtonStyle.Wide     => 312x48


GIDSignInButtonStyle.IconOnly =>  48x48

However, we may need custom styles other than the above sign-in buttons. For example the 48x48 IconOnly style is a bit small on the screen and hence we want to enlarge it. So the code below shows how to implement a custom Google sign-in button with these properties:


  • Custom image
  • Custom button size
  • Circular shape

To get started, implement a standard Google Sign-In button with instructions in this tutorial:

Google Sign-In for iOS - Create a GIDSignInButton programmatically in Swift

Then remove the GIDSignInButton and add a UIButton instead:


    var btnSignIn : UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        ...
        
        let btnSize : CGFloat = 100
        btnSignIn = UIButton(frame: CGRectMake(0,0,btnSize,btnSize))
        btnSignIn.center = view.center
        btnSignIn.setImage(UIImage(named: "google_logo.png"), forState: UIControlState.Normal)
        btnSignIn.addTarget(self, action: #selector(btnSignInPressed), forControlEvents: UIControlEvents.TouchUpInside)

        //Circular button
        btnSignIn.layer.cornerRadius = btnSize/2
        btnSignIn.layer.masksToBounds = true
        btnSignIn.layer.borderColor = UIColor.blackColor().CGColor
        btnSignIn.layer.borderWidth = 2
        view.addSubview(btnSignIn)

        ...
    }
    
    func btnSignInPressed() {
        GIDSignIn.sharedInstance().signIn()
    }

Result:


Related Information:

Facebook SDK and Swift - Create a custom login button programmatically

No comments:

Post a Comment