简体   繁体   中英

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 X

the separator is UIview I dragged and dropped it in storyboard and linked it to my controller

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM