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

时间:2015-07-16 12:02:31

标签: facebook swift ios8.3

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

2 个答案:

答案 0 :(得分:10)

我尝试了以下代码,它以正确的方式提供了功能。但是,在点击它时,我没有给按钮突出显示效果。 这是代码:

{
    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的自定义类,而swift并不认为它是uiButton,这就是为什么没有突出显示属性出现的原因。 因此,我的目标是帮助你找到它的方法是找到一种方法,让facebookLoginButton在swift中被视为uiButton。

答案 1 :(得分:7)

您可以使用默认的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]
            }
        })
    }
}