如何更改 FBSDKLoginButton iOS 8.3/Swift 1.2 的默认设计?

How can I change the default design of FBSDKLoginButton iOS 8.3/Swift 1.2?

我目前正在开发 App,我需要更改 Facebook SDK 提供的 Facebook 按钮的默认设计。 我成功地让提供的按钮透明并将其粘贴在设计视图上方,并且效果很好(现在设计匹配并且 Facebook SDK 按钮的功能运行良好)。 在我这样做之后我的问题出现了,因为按钮失去了它的 Ui 效果(点击时没有突出显示)。 如果有人可以帮助我,我需要为这个设计的按钮添加突出显示效果。 让我们澄清一下: 我将 UIView 设计为按钮,我在它上面放了一个透明的 Facebook SDK 按钮,结果是我的设计形状和 Facebook 按钮的功能同时存在,损失:点击时没有突出显示效果。

我试过下面的代码,它提供了正确的功能manner.However我没有在点击按钮时给按钮高亮效果。 这是代码:

{
    if (FBSDKAccessToken.currentAccessToken() != nil)
        {
            // if user logged in then handleTap func will run instead of button functionality
            let tap = UITapGestureRecognizer(target: self, action: "handleTap:")
            self.btnSignUpwithFaceBook.addGestureRecognizer(tap)

        }

        else
        {
            let loginView : FBSDKLoginButton = FBSDKLoginButton()
            self.view.addSubview(loginView)
            loginView.center = self.btnSignUpwithFaceBook.center
            loginView.frame.size.width = self.btnSignUpwithFaceBook.frame.width
            loginView.frame.size.height = self.btnSignUpwithFaceBook.frame.height
            loginView.frame.origin.x = self.btnSignUpwithFaceBook.frame.origin.x
            loginView.frame.origin.y = self.btnSignUpwithFaceBook.frame.origin.y

            for subView in loginView.subviews
            {
                subView.removeFromSuperview()
            }
            loginView.layer.shadowColor = UIColor.clearColor().CGColor

            loginView.setBackgroundImage(nil, forState: UIControlState.Normal)
            loginView.setBackgroundImage(nil, forState: UIControlState.Application)
            loginView.setBackgroundImage(nil, forState: UIControlState.allZeros)
            loginView.setBackgroundImage(nil, forState: UIControlState.Highlighted)
            loginView.setBackgroundImage(nil, forState: UIControlState.Reserved)
            loginView.setBackgroundImage(nil, forState: UIControlState.Selected)

            loginView.setImage(nil, forState: UIControlState.Normal)
            loginView.setImage(nil, forState: UIControlState.Application)
            loginView.setImage(nil, forState: UIControlState.allZeros)
            loginView.setImage(nil, forState: UIControlState.Highlighted)
            loginView.setImage(nil, forState: UIControlState.Reserved)
            loginView.setImage(nil, forState: UIControlState.Selected)

            loginView.backgroundColor = UIColor.clearColor()
            // just for test
            self.btnSignUpwithFaceBook.layer.borderWidth = 1
            self.btnSignUpwithFaceBook.layer.borderColor = UIColor.whiteColor().CGColor
            loginView.layer.backgroundColor = UIColor.clearColor().CGColor

            loginView.readPermissions = ["public_profile", "email", "user_friends"]
            loginView.delegate = self
            loginView.setTranslatesAutoresizingMaskIntoConstraints(false)

            var constX = NSLayoutConstraint(item: loginView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.btnSignUpwithFaceBook, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
            view.addConstraint(constX)

            var constY = NSLayoutConstraint(item: loginView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self.btnSignUpwithFaceBook, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
            view.addConstraint(constY)

            let views = ["loginView": loginView]

            var constH = NSLayoutConstraint.constraintsWithVisualFormat("H:[loginView(57)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views)
            view.addConstraints(constH)


            var constW = NSLayoutConstraint.constraintsWithVisualFormat("V:[loginView(281)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views)

            view.addConstraints(constW)

}

实际上主要问题是 facebookLogInButton 是为 FBSDK 定制的 class 并且 swift 不认为它是 uiButton 这就是为什么没有突出显示 属性 出现。 所以我的目的是让你们的帮助人员找到它的目的是找到一种方法,让我们将 facebookLoginButton 视为 swift.

中的 uiButton

您可以使用默认的UIButton,并使用FBSDKManager 代替Facebook SDK/FBSDKLoginButton登录。您可以在UIButton 上设置高亮状态颜色等。

从自定义 FB 按钮的 IBAction 调用方法 fbLoginInitiate

func fbLoginInitiate() {
    let loginManager = FBSDKLoginManager()
    loginManager.logInWithReadPermissions(["public_profile", "email"], handler: {(result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in
        if (error != nil) {
            // Process error
            self.removeFbData()
        } else if result.isCancelled {
            // User Cancellation
            self.removeFbData()
        } else {
            //Success
            if result.grantedPermissions.contains("email") && result.grantedPermissions.contains("public_profile") {
                //Do work
                self.fetchFacebookProfile()
            } else {
                //Handle error
            }
        }
    })
}

func removeFbData() {
    //Remove FB Data
    let fbManager = FBSDKLoginManager()
    fbManager.logOut()
    FBSDKAccessToken.setCurrentAccessToken(nil)
}

func fetchFacebookProfile()
{
    if FBSDKAccessToken.currentAccessToken() != nil {
        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

            if ((error) != nil) {
                //Handle error
            } else {
                //Handle Profile Photo URL String                        
                let userId =  result["id"] as! String
                let profilePictureUrl = "https://graph.facebook.com/\(id)/picture?type=large"

                let accessToken = FBSDKAccessToken.currentAccessToken().tokenString                        
                let fbUser = ["accessToken": accessToken, "user": result]
            }
        })
    }
}