[英]I want to place a button above another UI element programmatically in swift
I tried to place a button programmatically above a separator but it doesn't show at run time this is my view in story board shown in the picture 我试图以编程方式将按钮放置在分隔符上方,但在运行时并未显示,这是我在故事板上的视图
the separator is UIview I dragged and dropped it in storyboard and linked it to my controller 分隔符是UIview,我将其拖放到情节提要中并将其链接到我的控制器
this is my code // 这是我的代码//
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
}
}
but the button doesn't show at run time why? 但是按钮为什么在运行时不显示?
you need to activate your constraints, and make sure you added correctly 您需要激活约束,并确保添加正确
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.