快速设置LoginButton的约束

时间:2019-03-04 13:32:37

标签: swift

我想将我的Facebook身份验证登录按钮设置为我的视图底部。我试过了,但是不起作用:

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

不知道如何将其水平设置到视图中心的底部

2 个答案:

答案 0 :(得分:0)

您可以尝试

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)
])

答案 1 :(得分:0)

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

//设置约束

  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

如果程序上有任何约束,您甚至可以从UI界面(Storyboard或XIB)中添加该按钮。

简单地,使用@IBOutlet创建一个实例。 分配FBSDKLoginButton类,并将委托设置为该按钮。

@IBOutlet weak var loginButton: FBSDKLoginButton!

有关更多信息,请访问以下链接:Facebook Login button for Swift