Search code examples
swiftconstraintsfacebook-ios-sdk

Facebook button alignment issues


So I'm adding

Problem alignment

  var FBlogBut = FBSDKLoginButton()
    loginView.addSubview(FBlogBut)


    NSLayoutConstraint(item: FBlogBut, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 350 ).isActive = true


    NSLayoutConstraint(item: FBlogBut, attribute: .centerX, relatedBy: .equal, toItem: loginView, attribute: .centerX, multiplier: 1, constant: 0 ).isActive = true

    loginView.layoutIfNeeded()

So I'm not sure why the button is up in the top left corner and all the other items are working out ok..it at least should be 350 and cetnered.


Solution

  • There are two reasons.

    First, you are only setting a CenterX and a Width. You'll need to specify a vertical constraint, either a top constraint or a CenterY.

    The second is that by default translatesAutoresizingMaskIntoConstraints will be true, which means the view will set it's origin and size as constraints. This will mess your layout.

    Just set it to false: FBlogBut.translatesAutoresizingMaskIntoConstraints = false

    PS. If you want to follow naming conventions, variable names are camel cased, which means the first character is lowercase. In this case your abbreviations are unnecessary, the button name could be fbLoginButton and it's not too long. FBLogBut makes it unclear if it's a login or logout button.