0

i want to set my Login button for Facebook auth to the bottom of my View. i tried this but it doesnt work:

let lb = LoginButton(readPermissions: [ .email, .publicProfile ])
    lb.center = view.bottomAnchor

have no idea how to set it to bottom of view center horizontally

2 Answers2

0

You can try

view.addSubview(btn)
btn.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    btn.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
    btn.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
    btn.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
    btn.heightAnchor.constraint(equalToConstant: 50)  
])

OR

NSLayoutConstraint.activate([
    btn.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
    btn.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor) 
])

OR

NSLayoutConstraint.activate([
    btn.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
    btn.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
    btn.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.5),
    btn.heightAnchor.constraint(equalToConstant: 50)
])
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0
 let lb = UIButton()//LoginButton(readPermissions: [ .email, .publicProfile ])
        lb.center = self.view.center

//Set constraints

  NSLayoutConstraint.activate([
        lb.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), // centerXAnchor which set over the view center
        lb.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: 70.0), // BottomAnchor to set with botton target
        lb.widthAnchor.constraint(equalToConstant: 170.0), // Width Anchor to set appropriate width
        lb.heightAnchor.constraint(equalToConstant: 44.0) // Height Anchor to set appropriate hegiht of the button.
        ])

self.view.addSubview(lb)
lb.delegate = self

You can even add that button from your UI interface(Storyboard OR XIB) if any problem with programmatically constraints.

Simply, Create an instance with @IBOutlet. Assign FBSDKLoginButton class and set the delegate to that button.

@IBOutlet weak var loginButton: FBSDKLoginButton!

for more information please visit this link: Facebook Login button for Swift