我试图以编程方式在分隔符上方放置一个按钮,但它在运行时不显示这是我在故事板中的视图,如图所示
分隔符是UIview,我把它拖放到故事板中并链接到我的控制器上
这是我的密码
//
import UIKit
import Firebase
import FBSDKCoreKit
import FBSDKLoginKit
class LoginController: UIViewController, FBSDKLoginButtonDelegate {
@IBOutlet weak var separator: UIView!
@IBOutlet var fbLoginButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
if FBSDKAccessToken.current() != nil {
// Present the main view because user already logged in
self.performSegue(withIdentifier: "goToHome", sender: self)
}else{
loginButtonUI()
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
loginButtonUI()
}
private func loginButtonUI(){
let loginButton = FBSDKLoginButton()
loginButton.delegate = self
// Optional: Place the button in the center of your view.
loginButton.center = view.center
view.addSubview(loginButton)
}
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
// fb login code
}
}
但是按钮在运行时不显示为什么?
最佳答案
您需要激活约束,并确保正确添加了
let loginButton = FBSDKLoginButton()
loginButton.delegate = self
view.addSubview(loginButton)
loginButton.translatesAutoresizingMaskIntoConstraints = false
loginButton.centerXAnchor.constraint(equalTo: separatorView.centerXAnchor).isActive = true
loginButton.bottomAnchor.constraint(equalTo: separatorView.topAnchor, constant: 20).isActive = true
loginButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
loginButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
关于ios - 我想以编程方式快速将按钮放置在另一个UI元素上方,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56350571/