Cannot change the height of Login Button in FBSDKLoginKit?

后端 未结 12 1640
刺人心
刺人心 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:26

    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.

提交回复
热议问题