Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot change the height of Login Button in FBSDKLoginKit?

I am using FBSDKLoginKit in iOS with Swift.

Up until recently it has been working perfectly, however I now cannot override the height of my button in the Storyboard?

The height of the button is now much smaller for some reason. I have tried setting height constraints for the button, putting the button in a stack view and set to fill proportionally and even override the button height in the SDK with no luck.

If I change the button to a normal UIButton the layout constraints work perfectly.

This is what the button looks like when I run the app.

This is what the button looks like when I run the app.

This is how I would like the button to look - size wise.

This is how I would like the button to look - size wise.

like image 488
Ben Gilroy Avatar asked Feb 02 '17 12:02

Ben Gilroy


2 Answers

I've also run into this problem. The reason for this is explained in the 4.18.0 to 4.19.0 upgrade guide:

The FBSDKLoginButton UI has changed in 4.19.0. Instead of "Log in with Facebook", the button now displays "Continue with Facebook". The button color is changed to #4267B2 from #3B5998. The button height is now fixed at 28 due to use of smaller font size and paddings around a larger Facebook logo.

The only workaround I found so far is to downgrade the SDK version to 4.18.0 (it did the job for me).

It is possible that FB will address this issue (...that they've created for many people) in one of the future updates to the SDK.


Towards a more permanent solution, we can see the specific changes that caused this, on GitHub. The change I find most suspicious starts on line 194:

[self addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                 attribute:NSLayoutAttributeHeight
                                                 relatedBy:NSLayoutRelationEqual
                                                    toItem:nil
                                                 attribute:NSLayoutAttributeNotAnAttribute
                                                multiplier:1
                                                  constant:28]];

If the above constraint is removed/disabled, it could help reverse the situation. It should look approximately like this (I don't have an IDE at hand at the time of writing):

// Obtain all constraints for the button:
let layoutConstraintsArr = fbLoginButton.constraints
// Iterate over array and test constraints until we find the correct one:
for lc in layoutConstraintsArr { // or attribute is NSLayoutAttributeHeight etc.
   if ( lc.constant == 28 ){
     // Then disable it...
     lc.active = false
     break
   }
}

When I get a chance to test the above or if I find a better solution, I'll update the answer.

like image 173
Dev-iL Avatar answered Nov 17 '22 18:11

Dev-iL


You can conveniently achieve this with a simple override of the facebook button.

Swift:

class FacebookButton: FBSDKLoginButton {

    override func updateConstraints() {
        // deactivate height constraints added by the facebook sdk (we'll force our own instrinsic height)
        for contraint in constraints {
            if contraint.firstAttribute == .height, contraint.constant < standardButtonHeight {
                // deactivate this constraint
                contraint.isActive = false
            }
        }
        super.updateConstraints()
    }

    override var intrinsicContentSize: CGSize {
        return CGSize(width: UIViewNoIntrinsicMetric, height: standardButtonHeight)
    }

    override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
        let logoSize: CGFloat = 24.0
        let centerY = contentRect.midY
        let y: CGFloat = centerY - (logoSize / 2.0)
        return CGRect(x: y, y: y, width: logoSize, height: logoSize)
    }

    override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
        if isHidden || bounds.isEmpty {
            return .zero
        }

        let imageRect = self.imageRect(forContentRect: contentRect)
        let titleX = imageRect.maxX
        let titleRect = CGRect(x: titleX, y: 0, width: contentRect.width - titleX - titleX, height: contentRect.height)
        return titleRect
    }

}

In this code sample standardButtonHeight is a defined constant with the desired button height.

Also note that the logo size of 24.0 is the same size used in version 4.18 of the SDK.

like image 13
Christian Gossain Avatar answered Nov 17 '22 19:11

Christian Gossain