Cannot change the height of Login Button in FBSDKLoginKit?

后端 未结 12 1631
刺人心
刺人心 2021-02-05 09:19

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 St

相关标签:
12条回答
  • 2021-02-05 09:38

    This little autoclosure in Swift 4.0 also works perfectly if you have no reason to remove the constraint, just override it.

    let facebookLoginButton = FBSDKLoginButton()
    if let constraint = facebookLoginButton.constraints.first(where: { (constraint) -> Bool in
        return constraint.firstAttribute == .height
    }) {
        constraint.constant = 40.0
    }
    

    Or if you hate let statements:

    let facebookLoginButton = FBSDKLoginButton()
    facebookLoginButton.constraints.first(where: { (constraint) -> Bool in
        return constraint.firstAttribute == .height
    })?.constant = 40.0
    
    0 讨论(0)
  • 2021-02-05 09:39

    Xamarin.iOS example using Linq.

    I created the button in the storyboard file, and assigned a height there. However, I could not just remove all height constraints because the one I set in the storyboard was getting removed as well. I had to check to see if there is an existing height constraint of size 28 - and remove that one

    var contraint = this.FacebookLoginButton?.Constraints?.Where(x => x.FirstAttribute.Equals(NSLayoutAttribute.Height) && x.Constant == 28f)?.FirstOrDefault() ?? null;
    if (contraint != null)
    {
      this.FacebookLoginButton.RemoveConstraint(contraint);
    }
    
    0 讨论(0)
  • 2021-02-05 09:41

    Autolayout does not work on Facebook button(FBSDKButton) anymore. I changed its height using buttons frame.

        let fbLoginbutton = FBSDKLoginButton()
    
        view.addSubview(fbLoginbutton)
    
        fbLoginbutton.frame = CGRect(x: 38, y: 397, width: 300, height: 38)
    

    You can set it according to your requirement. Although I'm still not able to change its font & Logo size.

    0 讨论(0)
  • 2021-02-05 09:49

    Realising this is an old question, I've found a solution and am posting it as an answer for future reference.

    After you have installed the FBSDKLoginKit via a Pod, look in your directory on the left in XCODE and open your PODS -- not your Podfile. Open FBSDKLoginKit, and open FBSDKLoginButton.m file.

    You will now see an alert indicating that you are editing. Ignore the alert, that is take notice of the message and make sure that you don't change anything other than your target info. Change the two fields that dictate Facebook button height in the file as seen below:

    Pictures to help you through the guide above:

    • project structure files to open
    • first field to change
    • second field to change

    EASIEST SOLUTION, no need to deal with programmatic rects, just do it in storyboard

    0 讨论(0)
  • 2021-02-05 09:50

    As for now the Facebook button has only one constraint which is the height constraint and you can just remove all constraints of the button and add yours.

    facebookSignInButton.removeConstraints(facebookSignInButton.constraints)
    

    But of course this can change in the future and you might remove a constraint that you don't want to. Maybe a better solution would be if you remove only that problematic constraint.

    if let facebookButtonHeightConstraint = facebookSignInButton.constraints.first(where: { $0.firstAttribute == .height }) {
        facebookSignInButton.removeConstraint(facebookButtonHeightConstraint)
    }
    
    0 讨论(0)
  • 2021-02-05 09:52

    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.

    0 讨论(0)
提交回复
热议问题