限制Facebook登录按钮

时间:2018-07-24 15:25:51

标签: ios swift xcode facebook-login

我试图限制我的Facebook登录按钮,但宽度与其他按钮的宽度不匹配。

这是我的代码,用于限制Facebook登录按钮

var facebookButton = FBSDKLoginButton()

此代码在viewDidLayoutSubviews函数中

        // Constraints for button
for const in facebookButton.constraints{
        if const.firstAttribute == NSLayoutAttribute.height && const.constant == 28{
            facebookButton.removeConstraint(const)
        }
    }

facebookButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
//facebookButton.widthAnchor.constraint(equalTo: googleSignUpButton.widthAnchor).isActive = true
facebookButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 63)
facebookButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 64)
facebookButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
facebookButton.bottomAnchor.constraint(equalTo: googleSignUpButton.topAnchor, constant: -15).isActive = true

这是输出,我希望facebook按钮的宽度与其他按钮的宽度匹配。

Screenshot

2 个答案:

答案 0 :(得分:1)

执行以下操作之一:

1:为所有按钮设置固定宽度。

2:将前导锚和尾锚设置为Google按钮的前锚和尾锚。

3:使用UIStackView

答案 1 :(得分:0)

将此代码放入viewDidLoad

view.addSubview(facebookButton)
facebookButton.translatesAutoresizingMaskIntoConstraints = false
facebookButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
facebookButton.widthAnchor.constraint(equalTo: googleSignUpButton.widthAnchor).isActive = true
facebookButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
facebookButton.bottomAnchor.constraint(equalTo: googleSignUpButton.topAnchor, constant: -15).isActive = true