Facebook SDK自动更改高度

时间:2017-08-28 17:49:37

标签: ios swift facebook

我已经使用Facebook SDK在我的应用中实现了Facebook身份验证,但我遇到了登录按钮的奇怪行为。我已经设置了var motion = CMMotionManager() @IBOutlet weak var statusAccel: UILabel! override func viewDidAppear(_ animated: Bool) { motion.startAccelerometerUpdates(to: OperationQueue.current!){ (data , error) in if let trueData = data { self.view.reloadInputViews() self.statusAccel.text = "\(trueData)" if trueData.acceleration.z == 2 { // do things... } } } } 的约束,并将自定义类选择为UIButton,这可以正常工作。但是,当应用程序打开按钮时,将按钮的高度更改为其他内容。

How it is:

How It was suppose to be:

2 个答案:

答案 0 :(得分:1)

实施FB登录按钮的最佳方法是创建 OWN UIButton 。然后只需在按钮的IBAction中执行登录。这更容易,并提供了灵活性。

答案 1 :(得分:1)

只是为了更新这个。我已经解决了这个问题。我通过创建一个UIButton手动完成它,创建了自己的Outlet并添加了这个func

出口:

    @IBOutlet weak var facebookLoginBtn: UIButton!

所以

func configureFacebook(){

    let rect = CGRect(x: 0, y: 300, width: 300, height: 50)
    facebookLoginBtn.backgroundColor = UIColor(r: 75, g: 100, b: 157)
    facebookLoginBtn.frame = rect
    facebookLoginBtn.center = self.view.center
    facebookLoginBtn.setTitle("Login with Facebook", for: .normal)

    // Handle clicks on the button
    facebookLoginBtn.addTarget(self, action: #selector (self.loginButtonClicked), for: .touchUpInside)
    // Add the button to the view
    self.view.addSubview(facebookLoginBtn)
}

我还创建了一个功能,一旦用户登录Facebook就显示个人资料:

override func viewDidAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if(FBSDKAccessToken.current() != nil){

        let toProfileVC = storyboard?.instantiateViewController(withIdentifier: "profileNavigationController")
        self.present(toProfileVC!, animated: true, completion: nil)

    }else{

    }
}

现在,用Facebook登录

func loginButtonClicked(){

    var login = FBSDKLoginManager()
    //Login facebook permissions and prints
    login.logIn(withReadPermissions: ["public_profile", "email", "user_friends"], from: self) { (result: FBSDKLoginManagerLoginResult!, error: Error?) in
        if (error != nil){
            print(error)
        }

        if let userToken = result.token{
            let token : FBSDKAccessToken = result.token

            self.nextPage()
            print("Token = \(FBSDKAccessToken.current().tokenString)")
            print("User ID = \(FBSDKAccessToken.current().userID)")
        }
    }
}

func nextPage(){

    let toProfileVC = storyboard?.instantiateViewController(withIdentifier: "profileNavigationController")
    self.present(toProfileVC!, animated: true, completion: nil)
}

谢谢你们!